My Inorder Java solution


  • 0
    R
    class Solution {
        int min = Integer.MAX_VALUE;
        int secondMin = Integer.MAX_VALUE;
        public int findSecondMinimumValue(TreeNode root) {
            if(root == null) return 0;
            
            findSecondMinimumValue(root.left);
            
            if(root.val < min){
                secondMin = min;
                min = root.val;
            }
            else if(root.val < secondMin && root.val != min)
                secondMin = root.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.