Given a binary tree, find the vertical width of the binary tree. Width of a binary tree is the number of vertical paths.
Input : 7 / \ 6 5 / \ / \ 4 3 2 1 Output : 5 Input : 1 / \ 2 3 / \ / \ 4 5 6 7 \ \ 8 9 Output : 6
Prerequisite : Print Binary Tree in Vertical order
In this image, the tree contains 6 vertical lines which is the required width of tree.
Approach : In this Approach, we use the approach for printing vertical View of binary tree. Store the horizontal distances in a set and return 1 + highest horizontal distance – lowest horizontal distance. 1 is added to consider horizontal distance 0 as well. While going left, do hd – 1 and for right do hd + 1. We insert all possible distances in a hash table and finally return size of the hash table.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Vertical width of Binary tree | Set 1
- Vertical Sum in a given Binary Tree | Set 1
- Vertical Sum in Binary Tree | Set 2 (Space Optimized)
- Print a Binary Tree in Vertical Order | Set 1
- Find maximum vertical sum in binary tree
- Find if given vertical level of binary tree is sorted or not
- Print a Binary Tree in Vertical Order | Set 2 (Map based Method)
- Find the kth node in vertical order traversal of a Binary Tree
- 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
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.