Vertical order traversal of Binary Tree such that nodes are sorted individually
Given a binary tree, print it vertically.
NOTE: If there are multiple nodes at the same point, then print them in sorted order.
Examples:
Input: 1
/ \
2 3
/ \ / \
4 11 6 7
/ \
8 9
Output: [ [4], [2], [1, 6, 11], [3, 8], [7], [9] ]
Explanation: Traversing the tree vertically gives the above output.Input: 5
/ \
4 6
/ \ /
3 1 2
Output: [ [3], [4], [1, 2, 5], [6] ]
Approach: This problem is similar to Print a Binary Tree in Vertical Order. In that problem, if there are 2 nodes on the same vertical and on the same level then it is required to print from left to right, but this problem requires printing it in the sorted order. For that, take queue and map which consists of pair of integer and multiset to store multiple nodes that can have the same value as well.
Below is the implementation of the above approach.
C++
// C++ program for above 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 struct Node* newNode( int key) { struct Node* node = new Node; node->key = key; node->left = node->right = NULL; return node; } // Function to print vertical traversal // of a binary tree vector<vector< int > > printVerticalOrder(Node* root) { // map<vertical, map<level, // multiset<node values> > > map< int , map< int , multiset< int > > > mpp; // queue<Nodes, vertical, level> queue<pair<Node*, pair< int , int > > > q; q.push({ root, { 0, 0 } }); while (!q.empty()) { auto p = q.front(); q.pop(); Node* temp = p.first; // Vertical int vertical = p.second.first; // Level int level = p.second.second; // 2,0 -> {5,6} insert in the multiset mpp[vertical][level].insert(temp->key); // If left child of the node exits // then push it on the queue // with vertical decremented and // level incremented if (temp->left) q.push({ temp->left, { vertical - 1, level + 1 } }); // If right child of the node exits // then push it on the queue // with vertical incremented and // level incremented if (temp->right) q.push({ temp->right, { vertical + 1, level + 1 } }); } vector<vector< int > > ans; // Traverse the multiset part of each map for ( auto p : mpp) { vector< int > col; for ( auto q : p.second) { col.insert(col.end(), q.second.begin(), q.second.end()); } ans.push_back(col); } return ans; } // 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(11); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); root->right->right->right = newNode(9); // To store the vertical order traversal vector<vector< int > > v = printVerticalOrder(root); for ( auto i : v) { for ( auto j : i) { cout << j << " " ; } cout << endl; } return 0; } |
4 2 1 6 11 3 8 7 9
Time Complexity: O(N*logN*logN*logN)
Auxiliary Space: O(N)
Please Login to comment...