Simple Java Solution


  • 0
    S
        int secondMin = Integer.MAX_VALUE;
        public int findSecondMinimumValue(TreeNode root) {
            if(root.left==null && root.right==null)
                return secondMin==Integer.MAX_VALUE?-1:secondMin;
    
    
            if(root.val!=root.right.val && secondMin>root.right.val)
                secondMin = root.right.val;
            findSecondMinimumValue(root.left);
    
            if(root.val!=root.left.val && secondMin>root.left.val)
                secondMin = root.left.val;
            findSecondMinimumValue(root.right);
    
            return secondMin==Integer.MAX_VALUE?-1:secondMin;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.