Simple Java Solution With a Single Recursive Function (3ms)


  • 0
    A
    class Solution
    {
        public int findSecondMinimumValue(TreeNode root)
        {
            if(root.left == null)   return -1;
            
            int left  = (root.left.val  == root.val) ? findSecondMinimumValue(root.left)  : root.left.val;
            int right = (root.right.val == root.val) ? findSecondMinimumValue(root.right) : root.right.val;
    
            if(left  == -1)         return right;
            if(right == -1)         return left;
    
            return Math.min(left, right);
        }
    }
    

Log in to reply
 

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