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.
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the string formed by joining k consecutive nodes of linked list
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Find maximum among all right nodes in Binary Tree
- Sum of nodes at maximum depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Maximum weighted edge in path between two nodes in an N-ary tree using binary lifting
- Find maximum count of duplicate nodes in a Binary Search Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Maximum Consecutive Increasing Path Length in Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all nodes in a binary tree
- Sum of all leaf nodes of binary tree
- XOR of path between any two nodes in a Binary Tree
- Product of all nodes in a Binary Tree
- Sum of all nodes at Kth level in 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 email@example.com. 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.