Java DFS


  • 0
    L

    class Solution {
    public int findSecondMinimumValue(TreeNode root) {
    if(root == null){
    return -1;
    }

        return findSec(root, root.val);
    }
    private int findSec(TreeNode node, int val){
        if (node == null){
            return -1;
        }
        if (node.val != val){
            return node.val;
        }
        int le = findSec(node.left, val);
        int ri = findSec(node.right,val);
        if (le != -1 && ri != -1){
            return Math.min(le , ri);
        }
        if (le == -1){
            return ri;
        }
        return le;
    }

Log in to reply
 

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