leetcode-111. Minimum depth of binary tree

topic:

https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

answer:

Binary tree recursion:

 public int minDepth(TreeNode root) {

         if(root==null) return 0;

       if(root.left==null) return minDepth(root.right)+1;

       if(root.right==null) return minDepth(root.left)+1;

       int leftMinDepth = minDepth(root.left)+1;

       int rightMinDepth = minDepth(root.right)+1;

       return Math.min(leftMinDepth,rightMinDepth);

    }

Guess you like

Origin blog.csdn.net/wuqiqi1992/article/details/108343623