Traverse tree


  • 0
    O
    class Solution {
        int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
        public int findSecondMinimumValue(TreeNode root) {
            inorder(root);
            return second==Integer.MAX_VALUE?-1:second;
        }
    
        public void inorder(TreeNode root){
            if(root==null) return;
            inorder(root.left);
            if(root.val<first){
                second = first;
                first = root.val;
            }else if(root.val<second && root.val>first){
                second = root.val;
            }
            inorder(root.right);
        }
    }

Log in to reply
 

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