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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int maxOfRightElement(Node* root)
{
int res = INT_MIN;
if (root == NULL)
return -1;
if (root->right != NULL)
res = root->right->data;
return max({ maxOfRightElement(root->right),
res,
maxOfRightElement(root->left) });
}
int main()
{
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;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node left, right;
Node( int key)
{
data = key;
left = right = null ;
}
}
class GFG {
static int maxOfRightElement(Node root)
{
int res = Integer.MIN_VALUE;
if (root == null )
return - 1 ;
if (root.right != null )
res = root.right.data;
return Math.max(maxOfRightElement(root.right),
Math.max(res,maxOfRightElement(root.left)));
}
public static void main(String args[])
{
Node root = new Node( 7 );
root.left = new Node( 6 );
root.right = new Node( 5 );
root.left.left = new Node( 4 );
root.left.right = new Node( 3 );
root.right.left = new Node( 2 );
root.right.right = new Node( 1 );
System.out.println(maxOfRightElement(root));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
temp = Node( 0 )
temp.data = data
temp.left = temp.right = None
return temp
def maxOfRightElement(root):
res = - 999999
if (root = = None ):
return - 1
if (root.right ! = None ):
res = root.right.data
return max ( maxOfRightElement(root.right),
res,
maxOfRightElement(root.left) )
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 )
print (maxOfRightElement(root))
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int key)
{
data = key;
left = right = null ;
}
}
public class GFG
{
static int maxOfRightElement(Node root)
{
int res = int .MinValue;
if (root == null )
return -1;
if (root.right != null )
res = root.right.data;
return Math.Max(maxOfRightElement(root.right),
Math.Max(res,maxOfRightElement(root.left)));
}
public static void Main(String []args)
{
Node root = new Node(7);
root.left = new Node(6);
root.right = new Node(5);
root.left.left = new Node(4);
root.left.right = new Node(3);
root.right.left = new Node(2);
root.right.right = new Node(1);
Console.WriteLine(maxOfRightElement(root));
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .left = null ;
this .right = null ;
this .data = key;
}
}
function maxOfRightElement(root)
{
let res = Number.MIN_VALUE;
if (root == null )
return -1;
if (root.right != null )
res = root.right.data;
return Math.max(maxOfRightElement(root.right),
Math.max(res,maxOfRightElement(root.left)));
}
let root = new Node(7);
root.left = new Node(6);
root.right = new Node(5);
root.left.left = new Node(4);
root.left.right = new Node(3);
root.right.left = new Node(2);
root.right.right = new Node(1);
document.write(maxOfRightElement(root));
</script>
|
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space: O(n)
Iterative Approach(Using Level Order Traversal):
Follow the below steps to solve the above problem
1) Return -1 if root node is NULL or root->right node is NULL.
2) Perform Level Order Traversal and check at each node if right node is exist then store the maximum of ans and current->node->right node data in ans.
3) Finally after completely traversing return the res.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node( int data){
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
Node* newNode( int data){
return new Node(data);
}
int maxOfRightElement(Node* root){
int res = INT_MIN;
if (root == NULL) return -1;
if (root->right == NULL) return -1;
queue<Node*> q;
q.push(root);
while (!q.empty()){
Node* front_node = q.front();
q.pop();
if (front_node->left != NULL){
q.push(front_node->left);
}
if (front_node->right != NULL){
q.push(front_node->right);
res = max(res, front_node->right->data);
}
}
return res;
}
int main(){
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;
}
|
Java
import java.util.*;
class Node {
public int data;
public Node left, right;
public Node( int data) {
this .data = data;
left = right = null ;
}
}
class GFG {
static int maxOfRightElement(Node root) {
int res = Integer.MIN_VALUE;
if (root == null )
return - 1 ;
if (root.right == null )
return - 1 ;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node front_node = q.poll();
if (front_node.left != null ) {
q.add(front_node.left);
}
if (front_node.right != null ) {
q.add(front_node.right);
res = Math.max(res, front_node.right.data);
}
}
return res;
}
static public void main(String[] args) {
Node root = new Node( 7 );
root.left = new Node( 6 );
root.right = new Node( 5 );
root.left.left = new Node( 4 );
root.left.right = new Node( 3 );
root.right.left = new Node( 2 );
root.right.right = new Node( 1 );
System.out.println(maxOfRightElement(root));
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
return Node(data)
def maxOfRightElement(root):
res = - 10000000
if (root is None ):
return - 1
if (root.right is None ):
return - 1
q = []
q.append(root)
while ( len (q) > 0 ):
front_node = q.pop( 0 )
if (front_node.left is not None ):
q.append(front_node.left)
if (front_node.right is not None ):
q.append(front_node.right)
res = max (res, front_node.right.data)
return res
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 )
print (maxOfRightElement(root))
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
class GFG {
static int maxOfRightElement(Node root)
{
int res = int .MinValue;
if (root == null )
return -1;
if (root.right == null )
return -1;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count > 0) {
Node front_node = q.Dequeue();
if (front_node.left != null ) {
q.Enqueue(front_node.left);
}
if (front_node.right != null ) {
q.Enqueue(front_node.right);
res = Math.Max(res, front_node.right.data);
}
}
return res;
}
static public void Main()
{
Node root = new Node(7);
root.left = new Node(6);
root.right = new Node(5);
root.left.left = new Node(4);
root.left.right = new Node(3);
root.right.left = new Node(2);
root.right.right = new Node(1);
Console.WriteLine(maxOfRightElement(root));
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(data){
return new Node(data);
}
function maxOfRightElement(root){
let res = Number.MIN_VALUE;
if (root == null ) return -1;
if (root.right == null ) return -1;
let q = [];
q.push(root);
while (q.length > 0){
let front_node = q.shift();
if (front_node.left != null ) q.push(front_node.left);
if (front_node.right != null ){
q.push(front_node.right)
res = Math.max(res, front_node.right.data);
}
}
return res;
}
let 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);
console.log(maxOfRightElement(root));
|
Time Complexity : O(N), where N is the number of nodes.
Auxiliary Space: O(N), due to queue data structure.
Please Login to comment...