Java recursive solution


  • 0
    X
    public class Solution {
    public int minDepth(TreeNode root) {
       if(root ==null){
           return 0;
       }
       if(root.left ==null&&root.right == null){
           return 1;
       }
       if(root.left ==null){
           return 1+minDepth(root.right);
       }
       if(root.right ==null){
           return 1+minDepth(root.left);
       }
       int left = 1+minDepth(root.left);
       int right =1+ minDepth(root.right);
       return left<right?left:right;
    }
    

    }


  • 0
    D

    I think you mean recursive?


  • 0
    X

    sorry ,you are right!


Log in to reply
 

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