Fast and Accepted, but I don't get it. Java

  • 1

    public class Solution {

    public int minDepth(TreeNode root) {
        if(root == null){ return 0;}
        if(root.left == null){
            //I am not sure why it works for those node that are not the real root.
            return minDepth(root.right) + 1;
        else if(root.right == null ){
             return minDepth(root.left) + 1;
            int l = minDepth(root.left);
            int r = minDepth(root.right);
            return l<r?l+1:r+1;


  • 1

    Think about it, parameter "root" doesn't necessarily be the "real" root. Any node can be a root if you take he node as the root of the subtree.

Log in to reply

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