/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
int leftdepth = minDepth(root.left);
int rightdepth = minDepth(root.right);
if(root.left==null){
return rightdepth+1;
}
if(root.right==null){
return leftdepth+1;
}
return Math.min(leftdepth,rightdepth)+1;
}
}
111.二叉树的最小深度
最新推荐文章于 2024-08-18 22:40:28 发布