力扣111 9.6

111.二叉树的最小深度

练个手,我写的有点复杂,if语句要考虑到不同情况。不算难嗷,正常递归。

class Solution {

public int minDepth(TreeNode root) {

return dfs(root);

}

int dfs(TreeNode root){ if(root!=null&&root.left==null&&root.right==null){

return 1;

}

if(root==null){

return 0;

}

if(root.left!=null&&root.right!=null)

return Math.min(1+dfs(root.left),1+dfs(root.right));

else if(root.left!=null&&root.right==null)

return 1+dfs(root.left);

else if(root.right!=null&&root.left==null)

return 1+dfs(root.right);

return 0;

++好像是Java语法要求必须要有个return。++

}

}

相关推荐
he___H1 小时前
数据结构-移位
数据结构
电子_咸鱼1 小时前
LeetCode——Hot 100【电话号码的字母组合】
数据结构·算法·leetcode·链表·职场和发展·贪心算法·深度优先
仰泳的熊猫1 小时前
LeetCode:785. 判断二分图
数据结构·c++·算法·leetcode
rit84324991 小时前
基于MATLAB实现基于距离的离群点检测算法
人工智能·算法·matlab
my rainy days3 小时前
C++:友元
开发语言·c++·算法
haoly19893 小时前
数据结构和算法篇-归并排序的两个视角-迭代和递归
数据结构·算法·归并排序
微笑尅乐3 小时前
中点为根——力扣108.讲有序数组转换为二叉搜索树
算法·leetcode·职场和发展
小梁努力敲代码3 小时前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭4 小时前
构建Agent该选Python还是Java ?
java·python·agent
lang201509284 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端