Continuous Tree | C # 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
using System ; using System . Collections . Generic ; class GFG { public class node { public int val ; public node left ; public node right ; public node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } public node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } public node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static bool continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new Queue < node > ( ) ; Q . Enqueue ( root ) ; node temp ; while ( Q . Count != 0 ) { temp = Q . Peek ( ) ; Q . Dequeue ( ) ; if ( temp . left != null ) { if ( Math . Abs ( temp . left . val - temp . val ) == 1 ) Q . Enqueue ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . Abs ( temp . right . val - temp . val ) == 1 ) Q . Enqueue ( 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 ) ) Console . Write ( " True STRNEWLINE " ) ; else Console . Write ( " False STRNEWLINE " ) ; } }