-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathK_Distance_Nodes_Binary_Tree.cpp
175 lines (134 loc) · 3.49 KB
/
K_Distance_Nodes_Binary_Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Given a binary tree with unique values, a target node in the binary tree, and an integer value k, print all the nodes
that are at a distance k from the given target node.
Sample input :
Enter the size of preorder and inorder arrays : 8
Enter the elements of preorder traversal array : 1 2 3 4 8 5 6 7
Enter the elements of inorder traversal array : 3 2 8 4 1 6 7 5
Enter the target node in the binary tree : 2
Enter the value of distance K : 1
Sample output :
Nodes at a distance K are : 3 4 1
@author : shankhanil007
@created : 16-08-2020
*/
#include <iostream>
using namespace std;
// Basic definition of a node in binary tree
class node{
public:
int data;
node* left;
node* right;
node(int d)
{
data = d;
left = NULL;
right = NULL;
}
};
node* buildTree(int *preorder, int *inorder, int start, int end)
{
static int i = 0;
//Base Case
if(start > end){
return NULL;
}
//Recursive Case
node* newnode = new node(preorder[i]);
int index = -1;
for(int j = start; start <= end; j++)
{
if(preorder[i] == inorder[j])
{
index = j;
break;
}
}
i++;
newnode->left = buildTree(preorder, inorder, start, index-1);
newnode->right = buildTree(preorder, inorder, index+1, end);
return newnode;
}
// Function to print the Kth level of the tree starting from a given node
void printKthLevel(node* root, int k)
{
if(root == NULL)
return;
if(k == 0)
{
cout<<root->data<<" ";
return;
}
printKthLevel(root->left, k-1);
printKthLevel(root->right, k-1);
return;
}
// Function to find the nodes at a distance K from a given node
int printAtDistanceK(node* root, int target, int k)
{
// base case
if(root == NULL){
return -1;
}
//reach the target node
if(root->data == target){
printKthLevel(root , k);
return 0;
}
// next step - ancestor
int dl = printAtDistanceK(root->left, target, k);
if(dl != -1){
/* Again there are 2 cases
Ancestor itself or you need to go to the right ancestor */
if(dl+1 == k){
cout<<root->data<<" ";
}
else{
printKthLevel(root->right, k-2-dl);
}
return dl+1;
}
// next step - ancestor
int dr = printAtDistanceK(root->right, target, k);
if(dr != -1){
/* Again there are 2 cases
Ancestor itself or You need to go to the left ancestor */
if(dr+1 == k){
cout<<root->data<<" ";
}
else{
printKthLevel(root->left, k-2-dr);
}
return dr+1;
}
//node was not present in left and right subtree
return -1;
}
int main()
{
cout<<"Enter the size of preorder and inorder arrays"<<endl;
int n; cin>>n;
int preorder[n];
cout<<"Enter the elements of preorder traversal array"<<endl;
for(int i=0;i<n;i++)
{
cin>>preorder[i];
}
int inorder[n];
cout<<"Enter the elements of inorder traversal array"<<endl;
for(int i=0;i<n;i++)
{
cin>>inorder[i];
}
int target, k;
cout<<"Enter the target node in the binary tree"<<endl;
cin>>target;
cout<<"Enter the value of distance K"<<endl;
cin>>k;
node* root = buildTree(preorder, inorder, 0, n-1);
cout<<"\nNodes at a distance K are : "<<endl;
printAtDistanceK(root, target, k);
cout<<endl;
return 0;
}