-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_tree_nodes_at_k_distance.cpp
94 lines (81 loc) · 1.87 KB
/
B_tree_nodes_at_k_distance.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
#include<bits/stdc++.h>
using namespace std;
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class node
{
public:
int data;
node* left;
node* right;
/* Constructor that allocates a new node with the
given data and NULL left and right pointers. */
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
void printKDistance(node *root , int k)
{
//Practise Yourself : Write your code Here
if(root==NULL){
return;
}
if(k==0){
cout<<root->data<<" ";
return;
}
printKDistance(root->left,k-1);
printKDistance(root->right,k-1);
}
int main()
{
node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
root->right->left = new node(8);
printKDistance(root, 2);
return 0;
}
/* Try more Inputs
/* Constructed binary tree is
1
/ \
2 3
/ \ /
4 5 8
*/
/*Case 1:
Main tree = new Main();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(8);
tree.printLeftView(tree.root,2)
expected = [4,5,8]*/
//Case2:
/* Constructed binary tree is
10
/ \
12 3
\ /
4 5
\
6
*/
/*BinaryTree root = newNode(10);
root.left = newNode(12);
root.right = newNode(3);
root.left.right = newNode(4);
root.right.left = newNode(5);
root.right.left.right = newNode(6);
tree.printLeftView(root,2)
expected = [4,5]
*/