# Maximum length cycle that can be formed by joining two nodes of a binary tree

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 6Output:5 Cycle can be formed by joining node with value 5 and 6.Input:1 / \ 3 4 / \ 5 6 / \ 7 8 \ / 11 9Output: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++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Tree node structure ` `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); ` `} ` ` ` `// Function to find height of a tree ` `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); ` ` ` ` ` `// Update the answer, because diameter of a ` ` ` `// tree is nothing but maximum value of ` ` ` `// (left_height + right_height + 1) for each node ` ` ` `ans = max(ans, 1 + left_height + right_height); ` ` ` ` ` `return` `1 + max(left_height, right_height); ` `} ` ` ` `// Computes the diameter of binary tree ` `// with given root ` `int` `diameter(Node* root) ` `{ ` ` ` `if` `(root == NULL) ` ` ` `return` `0; ` ` ` ` ` `// Variable to store the final answer ` ` ` `int` `ans = INT_MIN; ` ` ` ` ` `int` `height_of_tree = height(root, ans); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of the approach

# Tree node structure

class Node:

def __init__(self, data):

self.data = data

self.left = None

self.right = None

# Function to find height of a tree

def height(root):

if root == None:

return 0

global ans

left_height = height(root.left)

right_height = height(root.right)

# Update the answer, because diameter of a

# tree is nothing but maximum value of

# (left_height + right_height + 1) for each node

ans = max(ans, 1 + left_height + right_height)

return 1 + max(left_height, right_height)

# Computes the diameter of

# binary tree with given root

def diameter(root):

if root == None:

return 0

height_of_tree = height(root)

return ans

# Driver code

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))

# This code is contributed by Rituraj Jain

**Output:**

4

## Recommended Posts:

- Find maximum among all right nodes in Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Sum of nodes at maximum depth of a Binary Tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Maximum Consecutive Increasing Path Length in Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Sum of all nodes in a binary tree
- XOR of path between any two nodes in a Binary Tree
- Product of all nodes in a Binary Tree
- Sum of all the Boundary Nodes of a Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.