Given a Binary Tree. The task is to find the maximum value among all of the right child nodes of the Binary Tree.
Note: If the tree does not contains any right child node or is empty, print -1.
Examples:
Input :
7
/ \
6 5
/ \ / \
4 3 2 1
Output : 5
All possible right child nodes are: {3, 5, 1}
out of which 5 is of the maximum value.
Input :
1
/ \
2 3
/ / \
4 5 6
\ / \
7 8 9
Output : 9
The idea is to recursively traverse the tree with inorder traversal and for every node:
- Check if the right child node exists.
- If yes, store it’s value in a temporary variable.
- Return the maximum among (current node’s right child node’s value, recursive call for left subtree, recursive call for right subtree).
Below is the implementation of the above approach:
// CPP program to print maximum element // among all right child nodes #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // Utility function to create a new tree node Node* newNode(int data) { Node* temp = new Node; temp->data = data; temp->left = temp->right = NULL; return temp; } // Function to find maximum element // among all right child nodes using // Inorder Traversal int maxOfRightElement(Node* root) { // Temp variable int res = INT_MIN; // If tree is empty if (root == NULL) return -1; // If right child exists if (root->right != NULL) res = root->right->data; // Return maximum of three values // 1) Recursive max in right subtree // 2) Value in right child node // 3) Recursive max in left subtree return max({ maxOfRightElement(root->right), res, maxOfRightElement(root->left) }); } // Driver Code int main() { // Create binary tree // as shown below /* 7 / \ 6 5 / \ / \ 4 3 2 1 */ Node* root = newNode(7); root->left = newNode(6); root->right = newNode(5); root->left->left = newNode(4); root->left->right = newNode(3); root->right->left = newNode(2); root->right->right = newNode(1); cout << maxOfRightElement(root); return 0; } |
chevron_right
filter_none
Output:
5
Recommended Posts:
- Sum of nodes at maximum depth of a 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 | Iterative Approach
- Find sum of all nodes of the given perfect binary tree
- Find distance between two nodes of a Binary Tree
- Queries to find distance between two nodes of a Binary tree
- Find height of a special binary tree whose leaf nodes are connected
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Find maximum vertical sum in binary tree
- Find maximum (or minimum) in Binary Tree
- Find maximum level sum in Binary Tree
- Find the node with maximum value in a Binary Search Tree
- Find maximum level product in Binary Tree
- Find the maximum path sum between two leaves of a binary tree
- Find the maximum node at a given 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 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.



