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.



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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a tree is continuous or not
#include<bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right



Output:

Yes

This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : andrew1234



Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.