Open In App

Vertical Traversal of a Binary Tree

Last Updated : 04 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a Binary Tree, the task is to find its vertical traversal starting from the leftmost level to the rightmost level. If multiple nodes pass through a vertical line, they should be printed as they appear in the level order traversal of the tree.

Examples:  

Input:

Vertical-Taversal-

Output: [[4], [2], [1, 5, 6], [3, 8], [7], [9]]
Explanation: The below image shows the horizontal distances used to print vertical traversal starting from the leftmost level to the rightmost level.

Vertical-Taversal--2

[Naive Approach] – O(n^2) Time and O(n) Space

The idea is to traverse the tree once and get the minimum and maximum horizontal distance with respect to root. For the tree shown above, minimum distance is -2 (for node with value 4) and maximum distance is 3 (For node with value 9). 
Once we have maximum and minimum distances from root, we iterate for each vertical line at distance minimum to maximum from root, and for each vertical line traverse the tree and return the nodes which lie on that vertical line. Please refer to Vertical Order using Brute Force for this approach.

[Expected Approach – 1] Using DFS and Hashmap – O(n) Time and O(n) Space

The idea is to traverse the tree using dfs and maintain a hashmap to store nodes at each horizontal distance (HD) from the root. Starting with an HD of 0 at the root, the HD is decremented for left children and incremented for right children. As we traverse, we add each node’s value to the map based on its HD. Finally, we collect the nodes from the map in increasing order of HD to produce the vertical order of the tree.

C++


// Helper function to perform DFS and 
// store nodes at different horizontal distances
void DFS(Node* root, int hd, int &mn, 
         		unordered_map<int, vector<int>> &mp) {
    if (root == nullptr) 
        return;
    
    // Store the current node in the map at horizontal distance hd
    mp[hd].push_back(root->data);
    
    // Update the minimum horizontal distance
    mn = min(mn, hd);
    
    // Recursively traverse the left and right subtrees
    DFS(root->left, hd - 1, mn, mp);  
    DFS(root->right, hd + 1, mn, mp);
}

// Function to perform vertical order traversal of a binary tree
vector<vector<int>> verticalOrder(Node *root) {
  
    // HashMap to store nodes at each horizontal distance
    unordered_map<int, vector<int>> mp;
    
    // Variable to track the minimum horizontal distance
    int mn = 0; 

    // Perform DFS to fill the hashmap with vertical levels
    DFS(root, 0, mn, mp);
    
    vector<vector<int>> res;
    int hd = mn;

    // Traverse the map from minimum to maximum horizontal distance
    while(mp.find(hd) != mp.end()) {
        res.push_back(mp[hd]);
        hd++;
    }
  
    return res;
}


Java Python C# JavaScript

Output
[ 4 ] [ 2 ] [ 1 5 6 ] [ 3 8 ] [ 7 ] [ 9 ] 

[Expected Approach – 2] Using BFS and Hashmap – O(n) Time and O(n) Space

This approach is similar to the above approach but here we traverse the tree using BFS traversal.
Refer Vertical Traversal of a Binary Tree using BFS for detailed explanation.

Related article:




Next Article

Similar Reads

three90RightbarBannerImg