Longest consecutive sequence in Binary tree
Given a Binary Tree find the length of the longest path which comprises of nodes with consecutive values in increasing order. Every node is considered as a path of length 1.
Examples:
In below diagram binary tree with longest consecutive path(LCP) are shown :
We can solve above problem recursively. At each node we need information of its parent node, if current node has value one more than its parent node then it makes a consecutive path, at each node we will compare node’s value with its parent value and update the longest consecutive path accordingly.
For getting the value of parent node, we will pass the (node_value + 1) as an argument to the recursive method and compare the node value with this argument value, if satisfies, update the current length of consecutive path otherwise reinitialize current path length by 1.
Please see below code for better understanding :
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void longestConsecutiveUtil(Node* root, int curLength,
int expected, int & res)
{
if (root == NULL)
return ;
if (root->data == expected)
curLength++;
else
curLength = 1;
res = max(res, curLength);
longestConsecutiveUtil(root->left, curLength,
root->data + 1, res);
longestConsecutiveUtil(root->right, curLength,
root->data + 1, res);
}
int longestConsecutive(Node* root)
{
if (root == NULL)
return 0;
int res = 0;
longestConsecutiveUtil(root, 0, root->data, res);
return res;
}
int main()
{
Node* root = newNode(6);
root->right = newNode(9);
root->right->left = newNode(7);
root->right->right = newNode(10);
root->right->right->right = newNode(11);
printf ( "%d\n" , longestConsecutive(root));
return 0;
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class Result
{
int res = 0 ;
}
class BinaryTree
{
Node root;
int longestConsecutive(Node root)
{
if (root == null )
return 0 ;
Result res = new Result();
longestConsecutiveUtil(root, 0 , root.data, res);
return res.res;
}
private void longestConsecutiveUtil(Node root, int curlength,
int expected, Result res)
{
if (root == null )
return ;
if (root.data == expected)
curlength++;
else
curlength = 1 ;
res.res = Math.max(res.res, curlength);
longestConsecutiveUtil(root.left, curlength, root.data + 1 , res);
longestConsecutiveUtil(root.right, curlength, root.data + 1 , res);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 6 );
tree.root.right = new Node( 9 );
tree.root.right.left = new Node( 7 );
tree.root.right.right = new Node( 10 );
tree.root.right.right.right = new Node( 11 );
System.out.println(tree.longestConsecutive(tree.root));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def longestConsecutiveUtil(root, curLength,
expected, res):
if (root = = None ):
return
if (root.data = = expected):
curLength + = 1
else :
curLength = 1
res[ 0 ] = max (res[ 0 ], curLength)
longestConsecutiveUtil(root.left, curLength,
root.data + 1 , res)
longestConsecutiveUtil(root.right, curLength,
root.data + 1 , res)
def longestConsecutive(root):
if (root = = None ):
return 0
res = [ 0 ]
longestConsecutiveUtil(root, 0 , root.data, res)
return res[ 0 ]
if __name__ = = '__main__' :
root = newNode( 6 )
root.right = newNode( 9 )
root.right.left = newNode( 7 )
root.right.right = newNode( 10 )
root.right.right.right = newNode( 11 )
print (longestConsecutive(root))
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class Result
{
public int res = 0;
}
class GFG
{
Node root;
int longestConsecutive(Node root)
{
if (root == null )
return 0;
Result res = new Result();
longestConsecutiveUtil(root, 0, root.data, res);
return res.res;
}
private void longestConsecutiveUtil(Node root, int curlength,
int expected, Result res)
{
if (root == null )
return ;
if (root.data == expected)
curlength++;
else
curlength = 1;
res.res = Math.Max(res.res, curlength);
longestConsecutiveUtil(root.left, curlength,
root.data + 1, res);
longestConsecutiveUtil(root.right, curlength,
root.data + 1, res);
}
public static void Main(String []args)
{
GFG tree = new GFG();
tree.root = new Node(6);
tree.root.right = new Node(9);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(10);
tree.root.right.right.right = new Node(11);
Console.WriteLine(tree.longestConsecutive(tree.root));
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data=item;
this .left = this .right = null ;
}
}
let res = 0;
let root;
function longestConsecutive(root)
{
if (root == null )
return 0;
res=[0];
longestConsecutiveUtil(root, 0, root.data, res);
return res[0];
}
function longestConsecutiveUtil(root,curlength, expected,res)
{
if (root == null )
return ;
if (root.data == expected)
curlength++;
else
curlength = 1;
res[0] = Math.max(res[0], curlength);
longestConsecutiveUtil(root.left, curlength,
root.data + 1, res);
longestConsecutiveUtil(root.right, curlength,
root.data + 1, res);
}
root = new Node(6);
root.right = new Node(9);
root.right.left = new Node(7);
root.right.right = new Node(10);
root.right.right.right = new Node(11);
document.write(longestConsecutive(root));
</script>
|
Time Complexity: O(N) where N is the Number of nodes in a given binary tree.
Auxiliary Space: O(log(N))
Also discussed on below link:
Maximum Consecutive Increasing Path Length in Binary Tree
Last Updated :
10 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...