Calculate depth of a full Binary tree from 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 program
function findDepthRec ( tree , n , index ) { if ( index >= n tree [ index ] == ' ' ) return 0 ; index ++ ; let left = findDepthRec ( tree , n , index ) ; index ++ ; let right = findDepthRec ( tree , n , index ) ; return Math . max ( left , right ) + 1 ; } function findDepth ( tree , n ) { let index = 0 ; return ( findDepthRec ( tree , n , index ) ) ; } let tree = " " . split ( ' ' ) ; let n = tree . length ; document . write ( findDepth ( tree , n ) ) ;