6 lines simple java code using dfs


  • 0
    J

    '''
    class Solution {
    public int findSecondMinimumValue(TreeNode root) {
    int res = larger(root);
    return res == Integer.MAX_VALUE ? -1 : res;
    }

    private int larger(TreeNode root){
        if(root == null || root.left == null){
            return Integer.MAX_VALUE;
        }
        int l = (root.val == root.left.val)  ? larger(root.left)  : root.left.val;
        int r = (root.val == root.right.val) ? larger(root.right) : root.right.val;
        return Math.min(l, r);
    }
    

    }
    '''


Log in to reply
 

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