Vertical width of Binary tree | Set 1 | Java program to print vertical width of a tree ; A Binary Tree Node ; get vertical width ; traverse left ; if curr is decrease then get value in minimum ; if curr is increase then get value in maximum ; traverse right ; 1 is added to include root in the width ; Utility function to create a new tree node ; Driver Code
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int maximum = 0 , minimum = 0 ; static void lengthUtil ( Node root , int curr ) { if ( root == null ) return ; lengthUtil ( root . left , curr - 1 ) ; if ( minimum > curr ) minimum = curr ; if ( maximum < curr ) maximum = curr ; lengthUtil ( root . right , curr + 1 ) ; } static int getLength ( Node root ) { maximum = 0 ; minimum = 0 ; lengthUtil ( root , 0 ) ; return ( Math . abs ( minimum ) + maximum ) + 1 ; } static Node newNode ( int data ) { Node curr = new Node ( ) ; curr . data = data ; curr . left = curr . right = null ; return curr ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . println ( getLength ( root ) ) ; } }