
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the kth node in vertical order traversal of a Binary Tree in C++
Suppose we have a binary tree and a value K. The task is to print the Kth node in the vertical order traversal. If no such node exists, then return -1. So if the tree is like below −
The vertical order traversal is like −
4 2 1 5 6 3 8 7 9
So if K = 3, then result will be 1.
The approach is simple. We will perform the vertical order traversal, then check the current node is the kth node or not, if so then return.
Example
#include<iostream> #include<map> #include<vector> #include<queue> using namespace std; class Node { public: int key; Node *left, *right; }; Node* getNode(int key){ Node* node = new Node; node->key = key; node->left = node->right = NULL; return node; } int findKthNodeVertical(Node* root, int k) { if (!root || k == 0) return -1; int n = 0; int k_node = -1; map<int, vector<int> > current_map; int hd = 0; queue<pair<Node*, int> > que; que.push(make_pair(root, hd)); while (!que.empty()) { pair<Node*, int> temp = que.front(); que.pop(); hd = temp.second; Node* node = temp.first; current_map[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)); } map<int, vector<int> >::iterator it; for (it = current_map.begin(); it != current_map.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; } int main() { Node* root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); root->right->left->right = getNode(8); root->right->right->right = getNode(9); int k = 3; cout << "Kth node in vertical traversal: " << findKthNodeVertical(root, k); }
Output
Kth node in vertical traversal: 1
- Related Articles
- Kth node in Diagonal Traversal of Binary Tree in C++
- Binary Tree Vertical Order Traversal in C++
- Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
- Find n-th node in Postorder traversal of a Binary Tree in C++
- Find n-th node in Preorder traversal of a Binary Tree in C++
- Binary Tree Level Order Traversal in C++
- Program to perform level order traversal of binary tree in C++
- Binary Tree Zigzag Level Order Traversal in Python
- C++ Program to Implement Double Order Traversal of a Binary Tree
- Find the Deepest Node in a Binary Tree in C++
- Diagonal Traversal of Binary Tree in C++?
- Program to find Kth ancestor of a tree node in Python
- Find mirror of a given node in Binary tree in C++
- Inorder Traversal of a Threaded Binary Tree in C++
- Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++

Advertisements