Continuous Tree

A tree is Continuous tree if in each root to leaf path, absolute difference between keys of two adjacent is 1. We are given a binary tree, we need to check if 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.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The solution requires a traversal of tree. The important things to check are to make sure that all corner cases are handled. The corner cases include, empty tree, single node tree, a node with only left child and a node with only right child.

In tree traversal, we recursively check if left and right subtree are continuous. We also check if difference between keys of current node’s key and its children keys is 1. Below is the implementation of the idea.

C++

 // C++ program to check if a tree is continuous or not #include using namespace std;    /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct Node {     int data;     struct Node* left, * right; };    /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct Node* newNode(int data) {     struct Node* node = new Node;     node->data = data;     node->left = node->right = NULL;     return(node); }    // Function to check tree is continuous or not bool treeContinuous(struct Node *ptr) {     // if next node is empty then return true     if (ptr == NULL)         return true;        // if current node is leaf node then return true     // because it is end of root to leaf path     if (ptr->left == NULL && ptr->right == NULL)         return true;        // If left subtree is empty, then only check right     if (ptr->left == NULL)        return (abs(ptr->data - ptr->right->data) == 1) &&               treeContinuous(ptr->right);        // If right subtree is empty, then only check left     if (ptr->right == NULL)        return (abs(ptr->data - ptr->left->data) == 1) &&               treeContinuous(ptr->left);        // If both left and right subtrees are not empty, check     // everything     return  abs(ptr->data - ptr->left->data)==1 &&             abs(ptr->data - ptr->right->data)==1 &&             treeContinuous(ptr->left) &&             treeContinuous(ptr->right); }    /* Driver program to test mirror() */ 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; }

/div>

Java

 // Java program to check if a tree is continuous or not import java.util.*;    class solution {    /* A binary tree node has data, pointer to left child and a pointer to right child */ static class Node {     int data;     Node left, right; };    /* Helper function that allocates a new node with the given data and null left and right pointers. */    static Node newNode(int data) {     Node node = new Node();     node.data = data;     node.left = node.right = null;     return(node); }    // Function to check tree is continuous or not    static boolean treeContinuous( Node ptr) {     // if next node is empty then return true     if (ptr == null)         return true;        // if current node is leaf node then return true     // because it is end of root to leaf path     if (ptr.left == null && ptr.right == null)         return true;        // If left subtree is empty, then only check right     if (ptr.left == null)     return (Math.abs(ptr.data - ptr.right.data) == 1) &&             treeContinuous(ptr.right);        // If right subtree is empty, then only check left     if (ptr.right == null)     return (Math.abs(ptr.data - ptr.left.data) == 1) &&             treeContinuous(ptr.left);        // If both left and right subtrees are not empty, check     // everything     return Math.abs(ptr.data - ptr.left.data)==1 &&             Math.abs(ptr.data - ptr.right.data)==1 &&             treeContinuous(ptr.left) &&             treeContinuous(ptr.right); }    /* Driver program to test mirror() */ 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"); } } //contributed by Arnab Kundu

C#

 // C# program to check if a tree is continuous or not  using System;    class solution  {     /* A binary tree node has data, pointer to left child  and a pointer to right child */ class Node  {      public int data;      public Node left, right;  };     /* Helper function that allocates a new node with the  given data and null left and right pointers. */    static Node newNode(int data)  {      Node node = new Node();      node.data = data;      node.left = node.right = null;      return(node);  }     // Function to check tree is continuous or not     static Boolean treeContinuous( Node ptr)  {      // if next node is empty then return true      if (ptr == null)          return true;         // if current node is leaf node then return true      // because it is end of root to leaf path      if (ptr.left == null && ptr.right == null)          return true;         // If left subtree is empty, then only check right      if (ptr.left == null)      return (Math.Abs(ptr.data - ptr.right.data) == 1) &&              treeContinuous(ptr.right);         // If right subtree is empty, then only check left      if (ptr.right == null)      return (Math.Abs(ptr.data - ptr.left.data) == 1) &&              treeContinuous(ptr.left);         // If both left and right subtrees are not empty, check      // everything      return Math.Abs(ptr.data - ptr.left.data)==1 &&              Math.Abs(ptr.data - ptr.right.data)==1 &&              treeContinuous(ptr.left) &&              treeContinuous(ptr.right);  }     /* Driver program to test mirror() */ 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");  }  }  //contributed by Arnab Kundu

Output:

Yes

Tree Tree