Longest Path with Same Values in a Binary Tree
Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of path between two nodes is represented by the number of edges between them.
Examples:
Input :
2
/ \
7 2
/ \ \
1 1 2
Output : 2
Input :
4
/ \
4 4
/ \ \
4 9 5
Output : 3
The idea is to recursively traverse given binary tree. We can think of any path (of nodes with the same values) in up to two directions(left and right) from it’s root. Then, for each node, we want to know what is the longest possible length extending in the left and the longest possible length extending in the right directions. The longest length that extends from the node will be 1 + length(node->left) if node->left exists, and has the same value as node. Similarly for the node->right case.
While we are computing lengths, each candidate answer will be the sum of the lengths in both directions from that node. We keep updating these answers and return the maximum one.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
struct Node *left, *right;
};
int length(Node *node, int *ans) {
if (!node)
return 0;
int left = length(node->left, ans);
int right = length(node->right, ans);
int Leftmax = 0, Rightmax = 0;
if (node->left && node->left->val == node->val)
Leftmax += left + 1;
if (node->right && node->right->val == node->val)
Rightmax += right + 1;
*ans = max(*ans, Leftmax + Rightmax);
return max(Leftmax, Rightmax);
}
int longestSameValuePath(Node *root) {
int ans = 0;
length(root, &ans);
return ans;
}
Node *newNode( int data) {
Node *temp = new Node;
temp->val = data;
temp->left = temp->right = NULL;
return temp;
}
int main() {
Node *root = NULL;
root = newNode(4);
root->left = newNode(4);
root->right = newNode(4);
root->left->left = newNode(4);
root->left->right = newNode(9);
root->right->right = newNode(5);
cout << longestSameValuePath(root);
return 0;
}
|
Java
class GFG
{
static int ans;
static class Node
{
int val;
Node left, right;
};
static int length(Node node)
{
if (node == null )
return 0 ;
int left = length(node.left);
int right = length(node.right);
int Leftmax = 0 , Rightmax = 0 ;
if (node.left != null &&
node.left.val == node.val)
Leftmax += left + 1 ;
if (node.right != null &&
node.right.val == node.val)
Rightmax += right + 1 ;
ans = Math.max(ans, Leftmax + Rightmax);
return Math.max(Leftmax, Rightmax);
}
static int longestSameValuePath(Node root)
{
ans = 0 ;
length(root);
return ans;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = temp.right = null ;
return temp;
}
public static void main(String[] args)
{
Node root = null ;
root = newNode( 4 );
root.left = newNode( 4 );
root.right = newNode( 4 );
root.left.left = newNode( 4 );
root.left.right = newNode( 9 );
root.right.right = newNode( 5 );
System.out.print(longestSameValuePath(root));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .val = data
self .left = self .right = None
def length(node, ans):
if ( not node):
return 0
left = length(node.left, ans)
right = length(node.right, ans)
Leftmax = 0
Rightmax = 0
if (node.left and node.left.val = = node.val):
Leftmax + = left + 1
if (node.right and node.right.val = = node.val):
Rightmax + = right + 1
ans[ 0 ] = max (ans[ 0 ], Leftmax + Rightmax)
return max (Leftmax, Rightmax)
def longestSameValuePath(root):
ans = [ 0 ]
length(root, ans)
return ans[ 0 ]
if __name__ = = '__main__' :
root = None
root = newNode( 4 )
root.left = newNode( 4 )
root.right = newNode( 4 )
root.left.left = newNode( 4 )
root.left.right = newNode( 9 )
root.right.right = newNode( 5 )
print (longestSameValuePath(root))
|
C#
using System;
class GFG
{
static int ans;
public class Node
{
public int val;
public Node left, right;
};
static int length(Node node)
{
if (node == null )
return 0;
int left = length(node.left);
int right = length(node.right);
int Leftmax = 0, Rightmax = 0;
if (node.left != null &&
node.left.val == node.val)
Leftmax += left + 1;
if (node.right != null &&
node.right.val == node.val)
Rightmax += right + 1;
ans = Math.Max(ans, Leftmax + Rightmax);
return Math.Max(Leftmax, Rightmax);
}
static int longestSameValuePath(Node root)
{
ans = 0;
length(root);
return ans;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = temp.right = null ;
return temp;
}
public static void Main(String[] args)
{
Node root = null ;
root = newNode(4);
root.left = newNode(4);
root.right = newNode(4);
root.left.left = newNode(4);
root.left.right = newNode(9);
root.right.right = newNode(5);
Console.Write(longestSameValuePath(root));
}
}
|
Javascript
<script>
let ans;
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .val = data;
}
}
function length(node)
{
if (node == null )
return 0;
let left = length(node.left);
let right = length(node.right);
let Leftmax = 0, Rightmax = 0;
if (node.left != null &&
node.left.val == node.val)
Leftmax += left + 1;
if (node.right != null &&
node.right.val == node.val)
Rightmax += right + 1;
ans = Math.max(ans, Leftmax + Rightmax);
return Math.max(Leftmax, Rightmax);
}
function longestSameValuePath(root)
{
ans = 0;
length(root);
return ans;
}
function newNode(data)
{
let temp = new Node(data);
temp.val = data;
temp.left = temp.right = null ;
return temp;
}
let root = null ;
root = newNode(4);
root.left = newNode(4);
root.right = newNode(4);
root.left.left = newNode(4);
root.left.right = newNode(9);
root.right.right = newNode(5);
document.write(longestSameValuePath(root));
</script>
|
Complexity Analysis:
- Time complexity: O(n), where n is the number of nodes in tree as every node is processed once.
- Auxiliary Space: O(h), where h is the height of tree as recursion can go upto depth h.
Last Updated :
04 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...