Given a binary tree, find the vertical width of the binary tree. The width of a binary tree is the number of vertical paths.
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 temporary variable if we go left then temp value decreases and if go to right then temp value increases. Assert a condition in this, if the minimum is greater than temp, then minimum = temp and if maximum less then temp then maximum = temp. In the end, print minimum + maximum which is the vertical width of the tree.
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Vertical width of Binary tree | Set 2
- Vertical Sum in a given Binary Tree | Set 1
- Find maximum vertical sum in binary tree
- Print a Binary Tree in Vertical Order | Set 1
- Vertical Sum in Binary Tree | Set 2 (Space Optimized)
- Find if given vertical level of binary tree is sorted or not
- Find the kth node in vertical order traversal of a Binary Tree
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Maximum width of a binary tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Vertical Zig-Zag traversal of a Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Check if a binary tree is subtree of another binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.