

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum length cycle that can be formed by joining two nodes of a binary tree in C++
We are given a binary tree. The goal is to find the maximum length cycle in the given tree. We will do this by finding the maximum height of the left subtree and right subtree from the root node and will join these maximum length paths to get the longest cycle.
For the above tree the maximum length cycle is 1-2-3-4-7-6 or 1-6-7-4-3-2-1. The length is 6.
Input − tree
Output − Maximum length cycle is − 5
Explanation − The max height of left subtree is 3 and of right subtree is 1. Cycle length becomes 3+1+1=5. Cycle is 1-2-3-4-6 or 1-6-4-3-2
Input − tree
Output − Maximum length cycle is − 7
Explanation − The max height of left subtree is 3 and of right subtree is 3. Cycle length becomes 3+3+1=7. Cycle is 5-4-2-1-8-7-6 or 5-6-7-8-1-2-4-5
Approach used in the below program is as follows
Create a class treenode which has public data members − int data for weight of the node, left and right treenode pointers to point to other such nodes.
Function newNode(int data) takes data as a parameter and creates a node with left and right pointers as NULL.
Create a tree by calling newnode() function.
Function maxheight(treenode* root) takes a root of the tree and returns the maximum height of the tree rooted at root.
This function checks if the root is NULL , means height is 0, return 0.
lheight and rheight calculates the heights of left and right subtree of node root, by recursive calls to maxheight(root->left); and maxheight(root->right);
Return the maximum value obtained by comparing lheight and rheight.
Inside main we store values of maximum height of left subtree and right subtree of tree Node.
Now the maximum length cycle is the sum of both maxlheight +maxrheight+1 for including the root itself.
Print the length of the cycle as a result.
Example
#include <bits/stdc++.h> using namespace std; //class for tree class treenode{ public: int data; treenode* left; treenode* right; }; //find maximum height between left and right subtree of current root int maxheight(treenode* root){ if (root == NULL) return 0; else{ int lheight = maxheight(root->left); int rheight = maxheight(root->right); //find maximum height if (lheight > rheight) return(lheight + 1); else return(rheight + 1); } } //creating a node of tree treenode* newNode(int data){ treenode* Node = new treenode(); Node->data = data; Node->left = NULL; Node->right = NULL; return(Node); } int main(){ treenode *root = newNode(6); root->left = newNode(8); root->right = newNode(9); root->left->left = newNode(4); root->left->right = newNode(5); root->left->right->right = newNode(7); root->left->right->right->left = newNode(2); int maxlheight=maxheight(root->left); int maxrheight=maxheight(root->right); int maxlheight=maxDepth(root->left); int maxrheight=maxDepth(root->right); cout << "Maximum length cycle: " << maxlheight+maxrheight+1; return 0; }
Output
Maximum length cycle: 6
- Related Questions & Answers
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Find distance between two nodes of a Binary Tree in C++
- Maximum possible time that can be formed from four digits in C++
- Find maximum number that can be formed using digits of a given number in C++
- Area of squares formed by joining midpoints repeatedly in C?
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- All possible strings of any length that can be formed from a given string?
- Find distance between two nodes of a Binary Tree in C++ Program
- Program to check whether two trees can be formed by swapping nodes or not in Python
- Validate Binary Tree Nodes in C++
- Maximum Consecutive Increasing Path Length in Binary Tree in C++
- Find maximum among all right nodes in Binary Tree in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Maximum money that can be withdrawn in two steps in C