
- 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
Binary Tree Vertical Order Traversal in C++
Suppose there is a binary tree, we have to find the vertical order traversal of its nodes' values. If two nodes are in the same row and column, the order should be from left to right.
So, if the input is like,
then the output will be [[9],[3,15],[20],[7]]
To solve this, we will follow these steps −
Define one map m
Define a function solve(), this will take node, x initialize it with 0,
if node is null, then −
return
solve(left of node, x - 1)
solve(right of node, x + 1)
insert value of node at the end of m[x]
From the main method, do the following −
if root is null, then −
return {}
Define one queue q
insert { 0, root } into q
insert value of node at the end of m[0]
while (q is not empty), do −
sz := size of q
while sz is non-zero, decrease sz in each iteration, do −
curr := first element of q
delete element from q
node = second element of curr
x := first element of curr
if left of node is not null, then −
insert { x - 1, left of node} into q
left of node at the end of m[x - 1]
if right of node is not null, then −
insert { x - 1, right of node} into q
right of node at the end of m[x - 1]
Define one 2D array ret
for each key-value pair 'it' of m do −
insert value of it into ret
return ret
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<vector<auto< > v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << "["; for(int j = 0; j <v[i].size(); j++){ cout << v[i][j] << ", "; } cout << "],"; } cout << "]"<<endl; } class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void insert(TreeNode **root, int val){ queue<TreeNode*> q; q.push(*root); while(q.size()){ TreeNode *temp = q.front(); q.pop(); if(!temp->left){ if(val != NULL) temp->left = new TreeNode(val); else temp->left = new TreeNode(0); return; } else{ q.push(temp->left); } if(!temp->right){ if(val != NULL) temp->right = new TreeNode(val); else temp->right = new TreeNode(0); return; } else{ q.push(temp->right); } } } TreeNode *make_tree(vector<int< v){ TreeNode *root = new TreeNode(v[0]); for(int i = 1; i<v.size(); i++){ insert(&root, v[i]); } return root; } class Solution { public: map<int, vector<int< > m; void solve(TreeNode* node, int x = 0){ if (!node || node->val == 0) return; solve(node->left, x - 1); solve(node->right, x + 1); m[x].push_back(node->val); } static bool cmp(vector<int<& a, vector<int<& b){ return a[0] != b[0] ? a[0] < b[0] : a[1] < b[1]; } vector<vector<int< > verticalOrder(TreeNode* root){ if (!root) return {}; queue<pair > q; q.push({ 0, root }); m[0].push_back(root->val); while (!q.empty()) { int sz = q.size(); while (sz--) { pair<int, TreeNode*> curr = q.front(); q.pop(); TreeNode* node = curr.second; int x = curr.first; if (node->left && node->left->val != 0) { q.push({ x - 1, node->left }); m[x - 1].push_back(node->left->val); } if (node->right && node->right->val != 0) { q.push({ x + 1, node->right }); m[x + 1].push_back(node->right->val); } } } vector<vector<int< > ret; map<int, vector<int< >::iterator it = m.begin(); while (it != m.end()) { ret.push_back(it->second); it++; } return ret; } }; main(){ Solution ob; vector<int< v = {3,9,20,NULL,NULL,15,7}; TreeNode *root = make_tree(v); print_vector(ob.verticalOrder(root)); }
Input
{3,9,20,NULL,NULL,15,7}
Output
[[9, ],[3, 15, ],[20, ],[7, ],]
- Related Articles
- Find the kth node in vertical order traversal of a Binary Tree in C++
- Binary Tree Level Order Traversal in C++
- Binary Tree Zigzag Level Order Traversal in Python
- Program to perform level order traversal of binary tree in C++
- C++ Program to Implement Double Order Traversal of a Binary Tree
- In-order traversal in Javascript Tree
- Binary Tree Inorder Traversal in Python
- Binary Tree Preorder Traversal in Python
- Binary Tree Postorder Traversal in Python
- Program to convert level order binary tree traversal to linked list in Python
- Level Order Tree Traversal in Data Structures
- Pre-order traversal in a Javascript Tree
- Binary Tree Postorder Traversal in Python Programming
- Diagonal Traversal of Binary Tree in C++?
- N-ary Tree Level Order Traversal in C++
