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