Continuous Tree | JAVA Code to check if the tree is continuos or not ; Node structure ; Function to check if the tree is continuos or not ; If root is Null then tree isn 't Continuos ; BFS Traversal ; Move to left child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Move to right child ; if difference between parent and child is equal to 1 then do continue otherwise make flag = 0 and break ; Driver Code ; Constructing the Tree ; Function Call
import java . util . * ; class GFG { static class node { int val ; node left ; node right ; node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static boolean continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new LinkedList < > ( ) ; Q . add ( root ) ; node temp ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; if ( temp . left != null ) { if ( Math . abs ( temp . left . val - temp . val ) == 1 ) Q . add ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . abs ( temp . right . val - temp . val ) == 1 ) Q . add ( temp . right ) ; else { flag = 0 ; break ; } } } if ( flag != 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { node root = new node ( 3 ) ; root . left = new node ( 2 ) ; root . right = new node ( 4 ) ; root . left . left = new node ( 1 ) ; root . left . right = new node ( 3 ) ; root . right . right = new node ( 5 ) ; if ( continuous ( root ) ) System . out . print ( "TrueNEW_LINE"); else System . out . print ( "FalseNEW_LINE"); } }