Maximum Consecutive Increasing Path Length in Binary Tree
Last Updated :
10 Mar, 2023
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:
10
/ \
/ \
11 9
/ \ /\
/ \ / \
13 12 13 8
Maximum Consecutive Path Length is 3 (10, 11, 12)
Note: 10, 9 ,8 is NOT considered since
the nodes should be in increasing order.
5
/ \
/ \
8 11
/ \
/ \
9 10
/ /
/ /
6 15
Maximum Consecutive Path Length is 2 (8, 9).
Every node in the Binary Tree can either become part of the path which is starting from one of its parent node or a new path can start from this node itself. The key is to recursively find the path length for the left and right sub tree and then return the maximum. Some cases need to be considered while traversing the tree which are discussed below.
- prev : stores the value of the parent node. Initialize prev with one less than value of root node so that the path starting at root can be of length at least 1.
- len : Stores the path length which ends at the parent of currently visited node.
Case 1: Value of Current Node is prev +1
In this case increase the path length by 1, and then recursively find the path length for the left and the right sub tree then return the maximum between two lengths.
Case 2: Value of Current Node is NOT prev+1
A new path can start from this node, so recursively find the path length for the left and the right sub tree. The path which ends at the parent node of current node might be greater than the path which starts from this node.So take the maximum of the path which starts from this node and which ends at previous node.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
Node *left, *right;
int val;
};
Node *newNode( int val)
{
Node *temp = new Node();
temp->val = val;
temp->left = temp->right = NULL;
return temp;
}
int maxPathLenUtil(Node *root, int prev_val, int prev_len)
{
if (!root)
return prev_len;
int cur_val = root->val;
if (cur_val == prev_val+1)
{
return max(maxPathLenUtil(root->left, cur_val, prev_len+1),
maxPathLenUtil(root->right, cur_val, prev_len+1));
}
int newPathLen = max(maxPathLenUtil(root->left, cur_val, 1),
maxPathLenUtil(root->right, cur_val, 1));
return max(prev_len, newPathLen);
}
int maxConsecutivePathLength(Node *root)
{
if (root == NULL)
return 0;
return maxPathLenUtil(root, root->val-1, 0);
}
int main()
{
Node *root = newNode(10);
root->left = newNode(11);
root->right = newNode(9);
root->left->left = newNode(13);
root->left->right = newNode(12);
root->right->left = newNode(13);
root->right->right = newNode(8);
cout << "Maximum Consecutive Increasing Path Length is "
<< maxConsecutivePathLength(root);
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node
{
Node left, right;
int val;
}
static Node newNode( int val)
{
Node temp = new Node();
temp.val = val;
temp.left = null ;
temp.right = null ;
return temp;
}
static int maxPathLenUtil(Node root, int prev_val, int prev_len)
{
if (root == null )
return prev_len;
int cur_val = root.val;
if (cur_val == prev_val+ 1 )
{
return Math.max(maxPathLenUtil(root.left, cur_val, prev_len+ 1 ),
maxPathLenUtil(root.right, cur_val, prev_len+ 1 ));
}
int newPathLen = Math.max(maxPathLenUtil(root.left, cur_val, 1 ),
maxPathLenUtil(root.right, cur_val, 1 ));
return Math.max(prev_len, newPathLen);
}
static int maxConsecutivePathLength(Node root)
{
if (root == null )
return 0 ;
return maxPathLenUtil(root, root.val- 1 , 0 );
}
public static void main(String[] args)
{
Node root = newNode( 10 );
root.left = newNode( 11 );
root.right = newNode( 9 );
root.left.left = newNode( 13 );
root.left.right = newNode( 12 );
root.right.left = newNode( 13 );
root.right.right = newNode( 8 );
System.out.println( "Maximum Consecutive Increasing Path Length is " +maxConsecutivePathLength(root));
}
}
|
Python3
class Node:
def __init__( self , val):
self .val = val
self .left = None
self .right = None
def maxPathLenUtil(root, prev_val, prev_len):
if root is None :
return prev_len
curr_val = root.val
if curr_val = = prev_val + 1 :
return max (maxPathLenUtil(root.left, curr_val, prev_len + 1 ),
maxPathLenUtil(root.right, curr_val, prev_len + 1 ))
newPathLen = max (maxPathLenUtil(root.left, curr_val, 1 ),
maxPathLenUtil(root.right, curr_val, 1 ))
return max (prev_len , newPathLen)
def maxConsecutivePathLength(root):
if root is None :
return 0
return maxPathLenUtil(root, root.val - 1 , 0 )
root = Node( 10 )
root.left = Node( 11 )
root.right = Node( 9 )
root.left.left = Node( 13 )
root.left.right = Node( 12 )
root.right.left = Node( 13 )
root.right.right = Node( 8 )
print ( "Maximum Consecutive Increasing Path Length is" ,)
print (maxConsecutivePathLength(root))
|
C#
using System;
class GfG
{
class Node
{
public Node left, right;
public int val;
}
static Node newNode( int val)
{
Node temp = new Node();
temp.val = val;
temp.left = null ;
temp.right = null ;
return temp;
}
static int maxPathLenUtil(Node root,
int prev_val, int prev_len)
{
if (root == null )
return prev_len;
int cur_val = root.val;
if (cur_val == prev_val+1)
{
return Math.Max(maxPathLenUtil(root.left, cur_val, prev_len+1),
maxPathLenUtil(root.right, cur_val, prev_len+1));
}
int newPathLen = Math.Max(maxPathLenUtil(root.left, cur_val, 1),
maxPathLenUtil(root.right, cur_val, 1));
return Math.Max(prev_len, newPathLen);
}
static int maxConsecutivePathLength(Node root)
{
if (root == null )
return 0;
return maxPathLenUtil(root, root.val - 1, 0);
}
public static void Main(String[] args)
{
Node root = newNode(10);
root.left = newNode(11);
root.right = newNode(9);
root.left.left = newNode(13);
root.left.right = newNode(12);
root.right.left = newNode(13);
root.right.right = newNode(8);
Console.WriteLine( "Maximum Consecutive" +
" Increasing Path Length is " +
maxConsecutivePathLength(root));
}
}
|
Javascript
<script>
class Node
{
constructor(val)
{
this .val = val;
this .left = this .right = null ;
}
}
function maxPathLenUtil(root,prev_val,prev_len)
{
if (root == null )
return prev_len;
let cur_val = root.val;
if (cur_val == prev_val+1)
{
return Math.max(maxPathLenUtil(root.left, cur_val, prev_len+1),
maxPathLenUtil(root.right, cur_val, prev_len+1));
}
let newPathLen = Math.max(maxPathLenUtil(root.left, cur_val, 1),
maxPathLenUtil(root.right, cur_val, 1));
return Math.max(prev_len, newPathLen);
}
function maxConsecutivePathLength(root)
{
if (root == null )
return 0;
return maxPathLenUtil(root, root.val-1, 0);
}
let root = new Node(10);
root.left = new Node(11);
root.right = new Node(9);
root.left.left = new Node(13);
root.left.right = new Node(12);
root.right.left = new Node(13);
root.right.right = new Node(8);
document.write( "Maximum Consecutive Increasing Path Length is " +
maxConsecutivePathLength(root)+ "<br>" );
</script>
|
Output
Maximum Consecutive Increasing Path Length is 3
Time Complexity: O(n^2), where n is the number of nodes in the given binary tree.
Auxiliary Space: O(log(n))
Share your thoughts in the comments
Please Login to comment...