Check if the Left View of the given tree is sorted or not | Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; Queue to hold values ; Variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; Variable for initial level ; Checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code
class node { constructor ( key ) { this . left = null ; this . right = null ; this . val = key ; } } function newnode ( key ) { let temp = new node ( key ) ; return temp ; } function func ( root ) { let q = [ ] ; let t = true ; q . push ( root ) ; let i = - 1 , j = - 1 ; while ( q . length > 0 ) { let h = q . length ; while ( h > 0 ) { root = q [ 0 ] ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . push ( root . left ) ; } if ( root . right != null ) { q . push ( root . right ) ; } h = h - 1 ; q . shift ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ;