|
| 1 | +/********************************************************** |
| 2 | +
|
| 3 | + Following is the Binary Tree Node class structure |
| 4 | +
|
| 5 | + template <typename T> |
| 6 | + class BinaryTreeNode { |
| 7 | + public : |
| 8 | + T data; |
| 9 | + BinaryTreeNode<T> *left; |
| 10 | + BinaryTreeNode<T> *right; |
| 11 | +
|
| 12 | + BinaryTreeNode(T data) { |
| 13 | + this -> data = data; |
| 14 | + left = NULL; |
| 15 | + right = NULL; |
| 16 | + } |
| 17 | + }; |
| 18 | +
|
| 19 | +***********************************************************/ |
| 20 | + |
| 21 | +void printkdistanceNodeDown(BinaryTreeNode<int>*root, int k) |
| 22 | +{ |
| 23 | + // Base Case |
| 24 | + if (root == NULL || k < 0) return; |
| 25 | + |
| 26 | + // If we reach a k distant node, print it |
| 27 | + if (k==0) |
| 28 | + { |
| 29 | + cout << root->data << endl; |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Recur for left and right subtrees |
| 34 | + printkdistanceNodeDown(root->left, k-1); |
| 35 | + printkdistanceNodeDown(root->right, k-1); |
| 36 | +} |
| 37 | +// Prints all nodes at distance k from a given target node. |
| 38 | +// The k distant nodes may be upward or downward. This function |
| 39 | +// Returns distance of root from target node, it returns -1 if target |
| 40 | +// node is not present in tree rooted with root. |
| 41 | + |
| 42 | + // Write your code here |
| 43 | +int printkdistanceNode(BinaryTreeNode<int>* root, int target , int k) |
| 44 | +{ |
| 45 | + // Base Case 1: If tree is empty, return -1 |
| 46 | + if (root == NULL) return -1; |
| 47 | + |
| 48 | + // If target is same as root. Use the downward function |
| 49 | + // to print all nodes at distance k in subtree rooted with |
| 50 | + // target or root |
| 51 | + if (root->data == target) |
| 52 | + { |
| 53 | + printkdistanceNodeDown(root, k); |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + // Recur for left subtree |
| 58 | + int dl = printkdistanceNode(root->left, target, k); |
| 59 | + |
| 60 | + // Check if target node was found in left subtree |
| 61 | + if (dl != -1) |
| 62 | + { |
| 63 | + // If root is at distance k from target, print root |
| 64 | + // Note that dl is Distance of root's left child from target |
| 65 | + if (dl + 1 == k) |
| 66 | + cout << root->data << endl; |
| 67 | + |
| 68 | + // Else go to right subtree and print all k-dl-2 distant nodes |
| 69 | + // Note that the right child is 2 edges away from left child |
| 70 | + else |
| 71 | + printkdistanceNodeDown(root->right, k-dl-2); |
| 72 | + |
| 73 | + // Add 1 to the distance and return value for parent calls |
| 74 | + return 1 + dl; |
| 75 | + } |
| 76 | + |
| 77 | + // MIRROR OF ABOVE CODE FOR RIGHT SUBTREE |
| 78 | + // Note that we reach here only when node was not found in left subtree |
| 79 | + int dr = printkdistanceNode(root->right, target, k); |
| 80 | + if (dr != -1) |
| 81 | + { |
| 82 | + if (dr + 1 == k) |
| 83 | + cout << root->data << endl; |
| 84 | + else |
| 85 | + printkdistanceNodeDown(root->left, k-dr-2); |
| 86 | + return 1 + dr; |
| 87 | + } |
| 88 | + |
| 89 | + // If target was neither present in left nor in right subtree |
| 90 | + return -1; |
| 91 | +} |
| 92 | +void nodesAtDistanceK(BinaryTreeNode<int> *root, int node, int k) { |
| 93 | + int x= printkdistanceNode(root,node,k); |
| 94 | +} |
0 commit comments