Error
|
|
---|---|
@Ibrahim Nash | 5765 |
@blackshadows | 5715 |
@akhayrutdinov | 5111 |
@mb1973 | 4993 |
@Quandray | 4944 |
@saiujwal13083 | 4506 |
@sanjay05 | 3762 |
@marius_valentin_dragoi | 3516 |
@sushant_a | 3459 |
@verma_ji | 3341 |
@KshamaGupta | 3318 |
Complete Leaderboard | |
|
|
@aroranayan999 | 1282 |
@bt8816103042 | 739 |
@rohitanand | 495 |
@codeantik | 479 |
@shalinibhataniya1097 | 472 |
@amrutakashikar2 | 464 |
@thealchemist627 | 456 |
@akashkale117 | 453 |
@shivanandp12345678910 | 453 |
@dalwainazism125 | 453 |
@SherlockHolmes3 | 447 |
Complete Leaderboard |
Given a Binary Tree of size N and an integer K. Print all nodes that are at distance k from root (root is considered at distance 0 from itself). Nodes should be printed from left to right. If k is more that height of tree, nothing should be printed.
For example, if below is given tree and k is 2. Output should be 4 5 6.
1
/ \
2 3
/ / \
4 5 6
\
8
Example 1:
Input:
K = 0
1
/ \
3 2
Output: 1
Example 2:
Input:
K = 3
3
/
2
\
1
/ \
5 3
Output: 5 3
Your Task:
You don't have to take input. Complete the function Kdistance() that accepts root node and k as parameter and return the value of the nodes that are at a distance k from the root. (The values are returned as vector<int> in cpp, as ArrayList<Integer> in java and list in python)
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Constraints:
1 <= N <= 104
0 <= K <= 30
Note: The Input/Ouput format and Example is given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?
Yes