Calculate depth of a full Binary tree from Preorder | Java program to find height of full binary tree using preorder ; function to return max of left subtree height or right subtree height ; calc height of left subtree ( In preorder left subtree is processed before right ) ; calc height of right subtree ; Wrapper over findDepthRec ( ) ; Driver Code
import java . io . * ; class GFG { static int findDepthRec ( String tree , int n , int index ) { if ( index >= n || tree . charAt ( index ) == ' l ' ) return 0 ; index ++ ; int left = findDepthRec ( tree , n , index ) ; index ++ ; int right = findDepthRec ( tree , n , index ) ; return Math . max ( left , right ) + 1 ; } static int findDepth ( String tree , int n ) { int index = 0 ; return ( findDepthRec ( tree , n , index ) ) ; } static public void main ( String [ ] args ) { String tree = " nlnnlll " ; int n = tree . length ( ) ; System . out . println ( findDepth ( tree , n ) ) ; } }