Check if the Left View of the given tree is sorted or not | Java implementation to 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
import java . util . * ; class GFG { static class node { int val ; node right , left ; } ; static node newnode ( int key ) { node temp = new node ( ) ; temp . val = key ; temp . right = null ; temp . left = null ; return temp ; } static void func ( node root ) { Queue < node > q = new LinkedList < > ( ) ; boolean t = true ; q . add ( root ) ; int i = - 1 , j = - 1 ; while ( ! q . isEmpty ( ) ) { int h = q . size ( ) ; while ( h > 0 ) { root = q . peek ( ) ; 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 . add ( root . left ) ; } if ( root . right != null ) { q . add ( root . right ) ; } h = h - 1 ; q . remove ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) System . out . print ( " true " + "NEW_LINE"); else System . out . print ( " false " + "NEW_LINE"); } public static void main ( String [ ] args ) { node 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 ) ; } }