Find the kth node in vertical order traversal of a Binary Tree
Given a binary tree and an integer k, the task is to print the kth node in the vertical order traversal of binary tree.If no such node exists then print -1.
The vertical order traversal of a binary tree means to print it vertically.
Examples:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
\ \
8 9
k = 3
Output: 1
The vertical order traversal of above tree is:
4
2
1 5 6
3 8
7
9
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
\ \
8 9
k = 13
Output: -1
Approach: The idea is to perform vertical order traversal and check if the current node is the kth node then print its value, if number of nodes in the tree is less than K then print -1.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Structure for a binary tree node struct Node { int key; Node *left, *right; }; // A utility function to create a new node Node* newNode(int key) { Node* node = new Node; node->key = key; node->left = node->right = NULL; return node; } // Function to find kth node // in vertcial order traversal int KNodeVerticalOrder(Node* root, int k) { // Base case if (!root || k == 0) return -1; int n = 0; // Variable to store kth node int k_node = -1; // Create a map and store vertical order in // map map<int, vector<int> > m; int hd = 0; // Create queue to do level order traversal // Every item of queue contains node and // horizontal distance queue<pair<Node*, int> > que; que.push(make_pair(root, hd)); while (!que.empty()) { // Pop from queue front pair<Node*, int> temp = que.front(); que.pop(); hd = temp.second; Node* node = temp.first; // Insert this node's data in vector of hash m[hd].push_back(node->key); if (node->left != NULL) que.push(make_pair(node->left, hd - 1)); if (node->right != NULL) que.push(make_pair(node->right, hd + 1)); } // Traverse the map and find kth // node map<int, vector<int> >::iterator it; for (it = m.begin(); it != m.end(); it++) { for (int i = 0; i < it->second.size(); ++i) { n++; if (n == k) return (it->second[i]); } } if (k_node == -1) return -1; } // Driver code int main() { Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); root->right->right->right = newNode(9); root->right->right->left = newNode(10); root->right->right->left->right = newNode(11); root->right->right->left->right->right = newNode(12); int k = 5; cout << KNodeVerticalOrder(root, k); return 0; } |
Python3
# Python3 implementation of the approach # Tree node structure class Node: def __init__(self, key): self.key = key self.left = None self.right = None # Function to find kth node # in vertcial order traversal def KNodeVerticalOrder(root, k): # Base case if not root or k == 0: return -1 n = 0 # Variable to store kth node k_node = -1 # Create a map and store # vertical order in map m = {} hd = 0 # Create queue to do level order # traversal Every item of queue contains # node and horizontal distance que = [] que.append((root, hd)) while len(que) > 0: # Pop from queue front temp = que.pop(0) hd = temp[1] node = temp[0] # Insert this node's data in vector of hash if hd not in m: m[hd] = [] m[hd].append(node.key) if node.left != None: que.append((node.left, hd - 1)) if node.right != None: que.append((node.right, hd + 1)) # Traverse the map and find kth node for it in sorted(m): for i in range(0, len(m[it])): n += 1 if n == k: return m[it][i] if k_node == -1: return -1 # Driver code if __name__ == "__main__": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) root.right.left.right = Node(8) root.right.right.right = Node(9) root.right.right.left = Node(10) root.right.right.left.right = Node(11) root.right.right.left.right.right = Node(12) k = 5 print(KNodeVerticalOrder(root, k)) # This code is contributed by Rituraj Jain |
6
Recommended Posts:
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Create a binary tree from post order traversal and leaf node array
- Find n-th node in Postorder traversal of a Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 1
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Flatten binary tree in order of post-order traversal
- Flatten Binary Tree in order of Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Flatten Binary Tree in order of Zig Zag traversal
- Kth node in Diagonal Traversal of Binary Tree
- Density of Binary Tree using Level Order Traversal
- Find maximum vertical sum in binary tree
- Perfect Binary Tree Specific Level Order Traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain


