Skip to content

Commit 9e292e1

Browse files
authored
Create Print Nodes at distance k from root
1 parent 95bc840 commit 9e292e1

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
You are given a Binary Tree of type integer, a target node, and an integer value K.
3+
Print the data of all nodes that have a distance K from the target node. The order in which they would be printed will not matter.
4+
5+
Input Format:
6+
The first line of input will contain the node data, all separated by a single space. Since -1 is used as an indication whether the left or right node data exist for root, it will not be a part of the node data.
7+
The second line of input contains two integers separated by a single space, representing the value of the target node and K, respectively.
8+
9+
Output Format:
10+
All the node data at distance K from the target node will be printed on a new line.
11+
The order in which the data is printed doesn't matter.
12+
13+
Constraints:
14+
1 <= N <= 10^5
15+
Where N is the total number of nodes in the binary tree.
16+
Time Limit: 1 sec
17+
18+
Sample Input 1:
19+
5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1
20+
3 1
21+
Sample Output 1:
22+
9
23+
6
24+
25+
Sample Input 2:
26+
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
27+
3 3
28+
Sample Output 2:
29+
4
30+
5
31+
*/
32+
33+
/*
34+
35+
Following is the structure used to represent the Binary Tree Node
36+
37+
class BinaryTreeNode<T> {
38+
T data;
39+
BinaryTreeNode<T> left;
40+
BinaryTreeNode<T> right;
41+
42+
public BinaryTreeNode(T data) {
43+
this.data = data;
44+
this.left = null;
45+
this.right = null;
46+
}
47+
}
48+
49+
*/
50+
51+
public class Solution {
52+
53+
public static void nodesAtDistanceK(BinaryTreeNode<Integer> root, int node, int k) {
54+
//Your code goes here
55+
print(root,node,k);
56+
57+
58+
}
59+
60+
private static int print(BinaryTreeNode<Integer> root, int node, int k)
61+
{
62+
//If tree is empty return -1
63+
if (root==null)
64+
return -1;
65+
66+
int rootData=root.data;
67+
if (rootData==node)
68+
{
69+
printNodesAtDistanceK(root, k);
70+
return 0;
71+
}
72+
73+
int leftSubTreeDist=0,rightSubTreeDist=0;
74+
75+
leftSubTreeDist=print(root.left,node,k);
76+
if (leftSubTreeDist!=-1)
77+
{
78+
if(leftSubTreeDist+1==k)
79+
{
80+
System.out.println(rootData);
81+
}
82+
else
83+
{
84+
rightSubTreeDist=k-(leftSubTreeDist+1)-1;
85+
printNodesAtDistanceK(root.right, rightSubTreeDist);
86+
}
87+
return leftSubTreeDist+1;
88+
}
89+
90+
rightSubTreeDist=print(root.right,node,k);
91+
if (rightSubTreeDist!=-1)
92+
{
93+
if(rightSubTreeDist+1==k)
94+
{
95+
System.out.println(rootData);
96+
}
97+
else
98+
{
99+
leftSubTreeDist=k-(rightSubTreeDist+1)-1;
100+
printNodesAtDistanceK(root.left, leftSubTreeDist);
101+
}
102+
return rightSubTreeDist+1;
103+
}
104+
return -1;
105+
}
106+
107+
private static void printNodesAtDistanceK(BinaryTreeNode<Integer> root, int k)
108+
{
109+
if (root==null || k<0)
110+
return;
111+
112+
if (k == 0)
113+
{
114+
System.out.println(root.data);
115+
return;
116+
}
117+
118+
printNodesAtDistanceK(root.left,k-1);
119+
printNodesAtDistanceK(root.right,k-1);
120+
}
121+
122+
}

0 commit comments

Comments
 (0)