Get maximum left node in binary tree

Given a tree, the task is to find the maximum in an only left node of the binary tree.

Examples:

Input : 
           7
         /    \
        6       5
       / \     / \
      4  3     2  1 
Output :
6

Input :
            1
         /    \
        2       3
       /       / \
      4       5   6
        \    /  \ 
         7  8   9 
Output :
8

Traverse with inorder traversal and Apply the condition for the left node only and get maximum of left node.
Let’s try to understand with code.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to print maximum element
// in left node.
#include <bits/stdc++.h>
using namespace std;
  
// A Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
  
// Get max of left element using
// Inorder traversal
int maxOfLeftElement(Node* root)
{
    int res = INT_MIN;
    if (root == NULL)
        return res;
  
    if (root->left != NULL)
        res = root->left->data;
  
    // Return maximum of three values
    // 1) Recursive max in left subtree
    // 2) Value in left node
    // 3) Recursive max in right subtree
    return max({ maxOfLeftElement(root->left),
                 res,
                 maxOfLeftElement(root->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;
}
  
// Driver program to test above functions
int main()
{
    // Let us create binary tree shown in above diagram
    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);
  
    /*     7
         /    \
        6       5
       / \     / \
      4  3     2  1          */
    cout << maxOfLeftElement(root);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print maximum element 
// in left node. 
import java.util.*;
class GfG {
  
// A Binary Tree Node 
static class Node { 
    int data; 
    Node left, right; 
}
  
// Get max of left element using 
// Inorder traversal 
static int maxOfLeftElement(Node root) 
    int res = Integer.MIN_VALUE; 
    if (root == null
        return res; 
  
    if (root.left != null
        res = root.left.data; 
  
    // Return maximum of three values 
    // 1) Recursive max in left subtree 
    // 2) Value in left node 
    // 3) Recursive max in right subtree 
    return Math.max(maxOfLeftElement(root.left),
       Math.max(res, maxOfLeftElement(root.right))); 
  
// Utility function to create a new tree node 
static Node newNode(int data) 
    Node temp = new Node(); 
    temp.data = data; 
    temp.left = null;
    temp.right = null
    return temp; 
  
// Driver program to test above functions 
public static void main(String[] args) 
    // Let us create binary tree shown in above diagram 
    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); 
  
    /* 7 
        / \ 
        6 5 
    / \ / \ 
    4 3 2 1         */
    System.out.println(maxOfLeftElement(root)); 
}

chevron_right


Python3

# Python program to prmaximum element
# in left node.

# Utility class to create a
# new tree node
class newNode:
def __init__(self, data):
self.data = data
self.left = self.right = None

# Get max of left element using
# Inorder traversal
def maxOfLeftElement(root):
res = -999999999999
if (root == None):
return res

if (root.left != None):
res = root.left.data

# Return maximum of three values
# 1) Recursive max in left subtree
# 2) Value in left node
# 3) Recursive max in right subtree
return max({ maxOfLeftElement(root.left), res,
maxOfLeftElement(root.right) })

# Driver Code
if __name__ == ‘__main__’:

# Let us create binary tree shown
# in above diagram
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)

# 7
# / \
# 6 5
# / \ / \
# 4 3 2 1
print(maxOfLeftElement(root))

# This code is contributed by PranchalK

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print maximum element 
// in left node.
using System;
  
class GfG 
{
  
    // A Binary Tree Node 
    class Node 
    
        public int data; 
        public Node left, right; 
    }
  
    // Get max of left element using 
    // Inorder traversal 
    static int maxOfLeftElement(Node root) 
    
        int res = int.MinValue; 
        if (root == null
            return res; 
  
        if (root.left != null
            res = root.left.data; 
  
        // Return maximum of three values 
        // 1) Recursive max in left subtree 
        // 2) Value in left node 
        // 3) Recursive max in right subtree 
        return Math.Max(maxOfLeftElement(root.left),
        Math.Max(res, maxOfLeftElement(root.right))); 
    
  
    // Utility function to create a new tree node 
    static Node newNode(int data) 
    
        Node temp = new Node(); 
        temp.data = data; 
        temp.left = null;
        temp.right = null
        return temp; 
    
  
    // Driver code 
    public static void Main(String[] args) 
    
        // Let us create binary tree
        // shown in above diagram 
        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); 
  
        /* 7 
            / \ 
            6 5 
        / \ / \ 
        4 3 2 1         */
        Console.WriteLine(maxOfLeftElement(root)); 
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.