力扣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。++

}

}

相关推荐
Java技术小馆7 分钟前
GitDiagram如何让你的GitHub项目可视化
java·后端·面试
学不动CV了10 分钟前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
Codebee24 分钟前
“自举开发“范式:OneCode如何用低代码重构自身工具链
java·人工智能·架构
weixin_4461224639 分钟前
LinkedList剖析
算法
程序无bug40 分钟前
手写Spring框架
java·后端
程序无bug42 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
全干engineer1 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
Fireworkitte1 小时前
Java 中导出包含多个 Sheet 的 Excel 文件
java·开发语言·excel
GodKeyNet1 小时前
设计模式-责任链模式
java·设计模式·责任链模式
a_Dragon11 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea