VOOZH about

URL: https://www.geeksforgeeks.org/dsa/width-binary-tree-set-1/

⇱ Vertical width of Binary tree | Set 1 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vertical width of Binary tree | Set 1

Last Updated : 11 Jul, 2025

Given a Binary tree, the task is to find the vertical width of the Binary tree. The width of a binary tree is the number of vertical paths in the Binary tree.
 
Examples: 

Input:

👁 Image

Output: 6
Explanation: In this image, the tree contains 6 vertical lines which are the required width of the tree.

Input : 

             7

           /  \

          6    5

         / \  / \

        4   3 2  1 

Output : 5

Input:

           1

         /    \

        2       3

       / \     / \

      4   5   6   7

               \   \ 

                8   9 

Output :  6

Approach:

Take inorder traversal and then take a temp variable to keep the track of unique vertical paths. when moving to left of the node then temp decreases by one and if goes to right then temp value increases by one. If the minimum is greater than temp, then update minimum = temp and if maximum less than temp then update maximum = temp. The vertical width of the tree will be equal to abs(minimum ) + maximum.

Follow the below steps to Implement the idea:

  • Initialize a minimum and maximum variable to track the left-most and right-most index.
  • Run Depth-first search traversal and maintain the current horizontal index curr, for root curr = 0.
    • Traverse left subtree with curr = curr-1
    • Update maximum with curr if curr > maximum.
    • Update minimum with curr if curr < minimum.
    • Traverse right subtree with curr = curr + 1.
  • Print the vertical width of the tree i.e. abs(minimum ) + maximum.

Below is the Implementation of the above approach:


Output
5

Time Complexity: O(n) 
Auxiliary Space: O(h) where h is the height of the binary tree. This much space is needed for recursive calls.

Find vertical width of Binary tree using Level Order Traversal:

Below is the idea to solve the problem:

Create a class to store the node and the horizontal level of the node. The horizontal level of left node will be 1 less than its parent, and horizontal level of the right node will be 1 more than its parent. We create a minLevel variable to store the minimum horizontal level or the leftmost node in the tree, and a maxLevel variable to store the maximum horizontal level or the rightmost node in the tree. Traverse the tree in level order and store the minLevel and maxLevel. In the end, print sum of maxLevel and absolute value of minLevel which is the vertical width of the tree.

Follow the below steps to Implement the idea:

  • Initialize two variables maxLevel = 0 and minLevel = 0 and queue Q of pair of the type (Node*, Integer).
  • Push (root, 0) in Q.
  • Run while loop till Q is not empty
    • Store the front node of the Queue in cur and the current horizontal level in count.
    • If curr->left is not null then push (root->left, count-1) and update minLevel with min(minLevel, count-1).
    • If curr->right is not null then push (root->right, count+1) and update maxLevel with max(maxLevel, count+1).
  • Print maxLevel + abs(minLevel) + 1.

Below is the Implementation of the above approach:


Output
5

Time Complexity: O(n). This much time is needed to traverse the tree.
Auxiliary Space: O(n). This much space is needed to maintain the queue.

Comment