A tree is a Continuous tree if in each root to leaf path, the absolute difference between keys of two adjacent is 1. We are given a binary tree, we need to check if the tree is continuous or not.
Examples:
Input : 3
/ \
2 4
/ \ \
1 3 5
Output: "Yes"
// 3->2->1 every two adjacent node's absolute difference is 1
// 3->2->3 every two adjacent node's absolute difference is 1
// 3->4->5 every two adjacent node's absolute difference is 1
Input : 7
/ \
5 8
/ \ \
6 4 10
Output: "No"
// 7->5->6 here absolute difference of 7 and 5 is not 1.
// 7->5->4 here absolute difference of 7 and 5 is not 1.
// 7->8->10 here absolute difference of 8 and 10 is not 1.
The solution requires a traversal of the tree. The important things to check are to make sure that all corner cases are handled. The corner cases include an empty tree, single-node tree, a node with only the left child and a node with the only right child.
In tree traversal, we recursively check if left and right subtree are continuous. We also check if the difference between the keys of the current node’s key and its children keys is 1. Below is the implementation of the idea.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, * right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
bool treeContinuous( struct Node *ptr)
{
if (ptr == NULL)
return true ;
if (ptr->left == NULL && ptr->right == NULL)
return true ;
if (ptr->left == NULL)
return ( abs (ptr->data - ptr->right->data) == 1) &&
treeContinuous(ptr->right);
if (ptr->right == NULL)
return ( abs (ptr->data - ptr->left->data) == 1) &&
treeContinuous(ptr->left);
return abs (ptr->data - ptr->left->data)==1 &&
abs (ptr->data - ptr->right->data)==1 &&
treeContinuous(ptr->left) &&
treeContinuous(ptr->right);
}
int main()
{
struct Node *root = newNode(3);
root->left = newNode(2);
root->right = newNode(4);
root->left->left = newNode(1);
root->left->right = newNode(3);
root->right->right = newNode(5);
treeContinuous(root)? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class solution
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static boolean treeContinuous( Node ptr)
{
if (ptr == null )
return true ;
if (ptr.left == null && ptr.right == null )
return true ;
if (ptr.left == null )
return (Math.abs(ptr.data - ptr.right.data) == 1 ) &&
treeContinuous(ptr.right);
if (ptr.right == null )
return (Math.abs(ptr.data - ptr.left.data) == 1 ) &&
treeContinuous(ptr.left);
return Math.abs(ptr.data - ptr.left.data)== 1 &&
Math.abs(ptr.data - ptr.right.data)== 1 &&
treeContinuous(ptr.left) &&
treeContinuous(ptr.right);
}
public static void main(String args[])
{
Node root = newNode( 3 );
root.left = newNode( 2 );
root.right = newNode( 4 );
root.left.left = newNode( 1 );
root.left.right = newNode( 3 );
root.right.right = newNode( 5 );
if (treeContinuous(root))
System.out.println( "Yes" ) ;
else
System.out.println( "No" );
}
}
|
Python
class newNode():
def __init__( self ,key) :
self .left = None
self .right = None
self .data = key
def treeContinuous(root):
if not root:
return True
if root.left:
if not abs (root.data - root.left.data) = = 1 :
return False
if root.right:
if not abs (root.data - root.right.data) = = 1 :
return False
if treeContinuous(root.left) and treeContinuous(root.right):
return True
if __name__ = = '__main__' :
root = newNode( 7 )
root.left = newNode( 6 )
root.right = newNode( 8 )
root.left.left = newNode( 5 )
root.left.right = newNode( 7 )
root.right.right = newNode( 7 )
print (treeContinuous(root))
|
C#
using System;
class solution
{
class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static Boolean treeContinuous( Node ptr)
{
if (ptr == null )
return true ;
if (ptr.left == null && ptr.right == null )
return true ;
if (ptr.left == null )
return (Math.Abs(ptr.data - ptr.right.data) == 1) &&
treeContinuous(ptr.right);
if (ptr.right == null )
return (Math.Abs(ptr.data - ptr.left.data) == 1) &&
treeContinuous(ptr.left);
return Math.Abs(ptr.data - ptr.left.data)==1 &&
Math.Abs(ptr.data - ptr.right.data)==1 &&
treeContinuous(ptr.left) &&
treeContinuous(ptr.right);
}
static public void Main(String []args)
{
Node root = newNode(3);
root.left = newNode(2);
root.right = newNode(4);
root.left.left = newNode(1);
root.left.right = newNode(3);
root.right.right = newNode(5);
if (treeContinuous(root))
Console.WriteLine( "Yes" ) ;
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data=0;
this .left = null ;
this .right = null ;
}
};
function newNode(data)
{
var node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
function treeContinuous( ptr)
{
if (ptr == null )
return true ;
if (ptr.left == null && ptr.right == null )
return true ;
if (ptr.left == null )
return (Math.abs(ptr.data - ptr.right.data) == 1) &&
treeContinuous(ptr.right);
if (ptr.right == null )
return (Math.abs(ptr.data - ptr.left.data) == 1) &&
treeContinuous(ptr.left);
return Math.abs(ptr.data - ptr.left.data)==1 &&
Math.abs(ptr.data - ptr.right.data)==1 &&
treeContinuous(ptr.left) &&
treeContinuous(ptr.right);
}
var root = newNode(3);
root.left = newNode(2);
root.right = newNode(4);
root.left.left = newNode(1);
root.left.right = newNode(3);
root.right.right = newNode(5);
if (treeContinuous(root))
document.write( "Yes" ) ;
else
document.write( "No" );
</script>
|
Another approach (using BFS(Queue))
Explanation: We will simply traverse each node level by level and check if the difference between parent and child is 1, if it is true for all nodes then we will return true else we will return false.
Implementation:
C++14
#include <bits/stdc++.h>
using namespace std;
struct node {
int val;
node* left;
node* right;
node()
: val(0)
, left(nullptr)
, right(nullptr)
{
}
node( int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
}
node( int x, node* left, node* right)
: val(x)
, left(left)
, right(right)
{
}
};
bool continuous( struct node* root)
{
if (root == NULL)
return false ;
int flag = 1;
queue< struct node*> Q;
Q.push(root);
node* temp;
while (!Q.empty()) {
temp = Q.front();
Q.pop();
if (temp->left) {
if ( abs (temp->left->val - temp->val) == 1)
Q.push(temp->left);
else {
flag = 0;
break ;
}
}
if (temp->right) {
if ( abs (temp->right->val - temp->val) == 1)
Q.push(temp->right);
else {
flag = 0;
break ;
}
}
}
if (flag)
return true ;
else
return false ;
}
int main()
{
struct 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))
cout << "True\n" ;
else
cout << "False\n" ;
return 0;
}
|
Java
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( "True\n" );
else
System.out.print( "False\n" );
}
}
|
Python3
class Node:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
def continuous(root):
if root is None :
return False
flag = 1
Q = []
Q.append(root)
temp = None
while len (Q) ! = 0 :
temp = Q.pop( 0 )
if temp.left is not None :
if abs (temp.left.val - temp.val) = = 1 :
Q.append(temp.left)
else :
flag = 0
break
if temp.right is not None :
if abs (temp.right.val - temp.val) = = 1 :
Q.append(temp.right)
else :
flag = 0
break
if flag ! = 0 :
return True
else :
return False
root = Node( 3 )
root.left = Node( 2 )
root.right = Node( 4 )
root.left.left = Node( 1 )
root.left.right = Node( 3 )
root.right.right = Node( 5 )
if continuous(root):
print ( "True" )
else :
print ( "False" )
|
C#
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\n" );
else
Console.Write( "False\n" );
}
}
|
Javascript
<script>
class Node
{
constructor(x)
{
this .val = x;
this .left = null ;
this .right= null ;
}
}
function continuous(root)
{
if (root == null )
return false ;
let flag = 1;
let Q = [];
Q.push(root);
let temp;
while (Q.length!=0)
{
temp = Q.shift();
if (temp.left != null )
{
if (Math.abs(temp.left.val - temp.val) == 1)
Q.push(temp.left);
else
{
flag = 0;
break ;
}
}
if (temp.right != null )
{
if (Math.abs(temp.right.val - temp.val) == 1)
Q.push(temp.right);
else
{
flag = 0;
break ;
}
}
}
if (flag != 0)
return true ;
else
return false ;
}
let 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))
document.write( "True<br>" );
else
document.write( "False<br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(N) ,here N is number of nodes in the tree.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...