What is the longest consecutive sequence in a Binary tree?

Share Your Love

For finding longest consecutive sequence in a binary tree, we first to know what is a binary tree?

What is a Binary Tree?

binary tree is either empty or consists of a node called the root together with two binary trees called the left subtree and the right subtree. The nodes of a binary tree can be numbered in a natural way, level by level, left to right.

Binay Tree
Binary Tree

For finding the longest consecutive sequence in a binary tree, suppose a Binary Tree find the length of the longest path which comprises nodes with consecutive values in increasing order. Every node is considered as a path of length 1.

Examples:

longest consecutive path in a binary tree
Longest Consecutive Path

In above diagram the longest consecutive path in Diagram-A is 1,2,3 => Length 3 and in Diagram-B is 9,10,11 => Length 3.

Now we can solve this problem in recursive rule.

At each node contains the information of parent node.

Then current node has value more than the parent node then it makes the consecutive path and each node compare its value with parent node value and update longest consecutive path accordingly.

So, how to get the value of parent node is a question now. 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 by length 1.

Now, please watch the below code for better understanding.

C++/C program to find the longest consecutive sequence in a binary tree

// C/C++ program to find longest consecutive
// sequence in binary tree
#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;
	Node *left, *right;
};

// A utility function to create a node
Node* newNode(int data)
{
	Node* temp = new Node;
	temp->data = data;
	temp->left = temp->right = NULL;
	return temp;
}

// Utility method to return length of longest
// consecutive sequence of tree
void longestConsecutiveUtil(Node* root, int curLength,
							int expected, int& res)
{
	if (root == NULL)
		return;

	// if root data has one more than its parent
	// then increase current length
	if (root->data == expected)
		curLength++;
	else
		curLength = 1;

	// update the maximum by current length
	res = max(res, curLength);

	// recursively call left and right subtree with
	// expected value 1 more than root data
	longestConsecutiveUtil(root->left, curLength,
						root->data + 1, res);
	longestConsecutiveUtil(root->right, curLength,
						root->data + 1, res);
}

// method returns length of longest consecutive
// sequence rooted at node root
int longestConsecutive(Node* root)
{
	if (root == NULL)
		return 0;

	int res = 0;

	// call utility method with current length 0
	longestConsecutiveUtil(root, 0, root->data, res);

	return res;
}

// Driver code to test above methods
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 program to find the longest consecutive sequence in a binary tree.

// Java program to find longest consecutive
// sequence in binary tree

class Node
{
	int data;
	Node left, right;

	Node(int item)
	{
		data = item;
		left = right = null;
	}
}

class Result
{
	int res = 0;
}

class BinaryTree
{
	Node root;

	// method returns length of longest consecutive
	// sequence rooted at node root
	int longestConsecutive(Node root)
	{
		if (root == null)
			return 0;

		Result res = new Result();
		
		// call utility method with current length 0
		longestConsecutiveUtil(root, 0, root.data, res);
		
		return res.res;
	}

	// Utility method to return length of longest
	// consecutive sequence of tree
	private void longestConsecutiveUtil(Node root, int curlength,
										int expected, Result res)
	{
		if (root == null)
			return;

		// if root data has one more than its parent
		// then increase current length
		if (root.data == expected)
			curlength++;
		else
			curlength = 1;

		// update the maximum by current length
		res.res = Math.max(res.res, curlength);

		// recursively call left and right subtree with
		// expected value 1 more than root data
		longestConsecutiveUtil(root.left, curlength, root.data + 1, res);
		longestConsecutiveUtil(root.right, curlength, root.data + 1, res);
	}

	// Driver code
	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));
	}
}

// This code is contributed by shubham96301

Python program to find the longest consecutive sequence in a binary tree.

# Python3 program to find longest consecutive
# sequence in binary tree

# A utility class to create a node
class newNode:
	def __init__(self, data):
		self.data = data
		self.left = self.right = None

# Utility method to return length of
# longest consecutive sequence of tree
def longestConsecutiveUtil(root, curLength,
						expected, res):
	if (root == None):
		return

	# if root data has one more than its
	# parent then increase current length
	if (root.data == expected):
		curLength += 1
	else:
		curLength = 1

	# update the maximum by current length
	res[0] = max(res[0], curLength)

	# recursively call left and right subtree
	# with expected value 1 more than root data
	longestConsecutiveUtil(root.left, curLength,
						root.data + 1, res)
	longestConsecutiveUtil(root.right, curLength,
						root.data + 1, res)

# method returns length of longest consecutive
# sequence rooted at node root
def longestConsecutive(root):
	if (root == None):
		return 0

	res = [0]

	# call utility method with current length 0
	longestConsecutiveUtil(root, 0, root.data, res)

	return res[0]

# Driver Code
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))

# This code is contributed by PranchalK

C# program to find the longest consecutive sequence in a binary tree.

// C# program to find longest consecutive
// sequence in binary tree
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;

	// method returns length of longest consecutive
	// sequence rooted at node root
	int longestConsecutive(Node root)
	{
		if (root == null)
			return 0;

		Result res = new Result();
		
		// call utility method with current length 0
		longestConsecutiveUtil(root, 0, root.data, res);
		
		return res.res;
	}

	// Utility method to return length of longest
	// consecutive sequence of tree
	private void longestConsecutiveUtil(Node root, int curlength,
										int expected, Result res)
	{
		if (root == null)
			return;

		// if root data has one more than its parent
		// then increase current length
		if (root.data == expected)
			curlength++;
		else
			curlength = 1;

		// update the maximum by current length
		res.res = Math.Max(res.res, curlength);

		// recursively call left and right subtree with
		// expected value 1 more than root data
		longestConsecutiveUtil(root.left, curlength,
							root.data + 1, res);
		longestConsecutiveUtil(root.right, curlength,
							root.data + 1, res);
	}

	// Driver code
	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));
	}
}

// This code is contributed by 29AjayKumar

Output:

3

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

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter