Easy to understand 1ms Java solution


  • 0
    T
    public int minDepth(TreeNode root) {
            if (null == root) {
                return 0;
            }
    
            int l = minDepth(root.left);
            int r = minDepth(root.right);
            if (l == 0 && r == 0) {
                return 1;
            } else if (l == 0 && r != 0) {
                return r + 1;
            } else if (l != 0 && r == 0) {
                return l + 1;
            } else{
                int min = l < r ? l : r;
                return min + 1;
            }
        }```

Log in to reply
 

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