Maximum length cycle that can be formed by joining two nodes of a binary tree Last Updated : 05 Aug, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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++ // 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; } Java // Java implementation of the approach class GFG { // Tree node structure 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); } // Function to find height of a tree static int height(Node root) { if (root == null) return 0; int left_height = height(root.left); int 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 = Math.max(ans, 1 + left_height + right_height); return 1 + Math.max(left_height, right_height); } // Computes the diameter of binary tree // with given root static int diameter(Node root) { if (root == null) return 0; // Variable to store the final answer ans = Integer.MIN_VALUE; int height_of_tree = height(root); return ans; } // Driver code 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)); } } // This code is contributed by Princi Singh 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 C# // C# implementation of the approach using System; class GFG { // Tree node structure 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); } // Function to find height of a tree static int height(Node root) { if (root == null) return 0; int left_height = height(root.left); int 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 = Math.Max(ans, 1 + left_height + right_height); return 1 + Math.Max(left_height, right_height); } // Computes the diameter of binary tree // with given root static int diameter(Node root) { if (root == null) return 0; // Variable to store the final answer ans = int.MinValue; int height_of_tree = height(root); return ans; } // Driver code 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)); } } // This code is contributed by Rajput-Ji JavaScript <script> // JavaScript implementation of the approach 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 to find height of a tree function height(root) { if (root == null) return 0; let left_height = height(root.left); let 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 = Math.max(ans, 1 + left_height + right_height); return 1 + Math.max(left_height, right_height); } // Computes the diameter of binary tree // with given root function diameter(root) { if (root == null) return 0; // Variable to store the final answer 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> Output: 4 Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Maximum height of Tree when any Node can be considered as Root S sakshi_srivastava Follow Improve Article Tags : DSA Binary Tree Similar Reads Maximum height of Tree when any Node can be considered as Root Given a tree with n nodes and n-1 edges, the task is to find the maximum height of the tree when any node in the tree is considered as the root of the tree. Example:Input: Output: 5Explanation: The below diagram represents a tree with 11 nodes and 10 edges and the path that gives us the maximum heig 15+ min read Construct a Maximum Binary Tree from two given Binary Trees Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree. What is the maximum Binary Tree? The maximum binary is constructed in the following manner: In the case of both the Binary Trees having two corresponding 8 min read Maximum sum of nodes in Binary tree such that no two are adjacent Given a binary tree with a value associated with each node, we need to choose a subset of these nodes such that the sum of selected nodes is maximum under a constraint that no two chosen nodes in the subset should be directly connected, that is, if we have taken a node in our sum then we canât take 15+ min read Find the maximum node at a given level in a binary tree Given a Binary Tree and a Level. The task is to find the node with the maximum value at that given level. The idea is to traverse the tree along depth recursively and return the nodes once the required level is reached and then return the maximum of left and right subtrees for each subsequent call. 13 min read Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions Given an array arr[] of (N - 1) integers and each value arr[i](1-based indexing) is the score of the nodes having degree i. The task is to determine the maximum score of any tree of N nodes that can be constructed. Examples: Input: arr[] = {1, 3, 0}Output: 8Explanation:One possible way to construct 14 min read Maximum Consecutive Increasing Path Length in Binary Tree 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 co 10 min read Like