0
点赞
收藏
分享

微信扫一扫

LeetCode 二叉树的最小深度

棒锤_45f2 2022-02-28 阅读 58


我的解决方案:

import javax.print.attribute.standard.RequestingUserName;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
//判断是否为叶子结点
if(root.left==null&&root.right==null)
return 1;
int res1=Integer.MAX_VALUE;
int res2=Integer.MAX_VALUE;
//对空子树不进行处理,将其设置为最大值
if(root.left!=null)
res1 = minDepth(root.left);
if(root.right!=null)
res2 = minDepth(root.right);
return Integer.min(res1, res2)+1;
}
}



举报

相关推荐

0 条评论