Maximum length cycle that can be formed by joining two nodes of a binary tree
Last Updated :
05 Aug, 2021
Given a binary tree, the task is to find the maximum length of the cycle that can be formed by joining any two nodes of the tree.
Examples:
Input:
1
/ \
2 3
\ \
5 6
Output: 5
Cycle can be formed by joining node with value 5 and 6.
Input:
1
/ \
3 4
/ \
5 6
/ \
7 8
\ /
11 9
Output: 7
Approach: The idea is to find the diameter of the given binary tree, since cycle with maximum length will be equal to the diameter of the binary tree.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int height(Node* root, int & ans)
{
if (root == NULL)
return 0;
int left_height = height(root->left, ans);
int right_height = height(root->right, ans);
ans = max(ans, 1 + left_height + right_height);
return 1 + max(left_height, right_height);
}
int diameter(Node* root)
{
if (root == NULL)
return 0;
int ans = INT_MIN;
int height_of_tree = height(root, ans);
return ans;
}
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf ( "%d" , diameter(root));
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node left, right;
};
static int ans;
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static int height(Node root)
{
if (root == null )
return 0 ;
int left_height = height(root.left);
int right_height = height(root.right);
ans = Math.max(ans, 1 + left_height + right_height);
return 1 + Math.max(left_height, right_height);
}
static int diameter(Node root)
{
if (root == null )
return 0 ;
ans = Integer.MIN_VALUE;
int height_of_tree = height(root);
return ans;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
System.out.printf( "%d" , diameter(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def height(root):
if root = = None :
return 0
global ans
left_height = height(root.left)
right_height = height(root.right)
ans = max (ans, 1 + left_height + right_height)
return 1 + max (left_height, right_height)
def diameter(root):
if root = = None :
return 0
height_of_tree = height(root)
return ans
if __name__ = = "__main__" :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 5 )
ans = 0
print (diameter(root))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static int ans;
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static int height(Node root)
{
if (root == null )
return 0;
int left_height = height(root.left);
int right_height = height(root.right);
ans = Math.Max(ans, 1 + left_height +
right_height);
return 1 + Math.Max(left_height,
right_height);
}
static int diameter(Node root)
{
if (root == null )
return 0;
ans = int .MinValue;
int height_of_tree = height(root);
return ans;
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
Console.WriteLine( "{0}" , diameter(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
let ans;
function newNode(data)
{
let node = new Node(data);
return (node);
}
function height(root)
{
if (root == null )
return 0;
let left_height = height(root.left);
let right_height = height(root.right);
ans = Math.max(ans, 1 + left_height + right_height);
return 1 + Math.max(left_height, right_height);
}
function diameter(root)
{
if (root == null )
return 0;
ans = Number.MIN_VALUE;
let height_of_tree = height(root);
return ans;
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
document.write(diameter(root));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...