Java simple DFS Solution


  • 0
    J
    public int findSecondMinimumValue(TreeNode root) {
            if(root == null)
                return -1;
            return helper(root, root.val);
        }
        public int helper(TreeNode node, int val){
            if(node == null)
                return -1;
            if(node.val != val)
                return node.val;
            int left = helper(node.left, val);
            int right = helper(node.right, val);
            if(left == -1 || right == -1){
                return Math.max(left, right);
            }
            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.