LeetCode | Minimum Depth of Binary Tree
题目:给定一个二叉树,找到其最小深度。最小深度是从根节点到最近叶节点的最短路径的节点数。
1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 /** 11 最小深度为:根节点到达最近叶节点的最短路径 12 总体思想是:左子树最小深度+1,右子树最小深度+1,取小的一个 13 需要考虑:当树只有一个子树的情况下,最小深度为:左右子树的最大值 14 */ 15 public class Solution { 16 public int run(TreeNode root) { 17 if(root == null) 18 return 0; 19 20 int left = run(root.left) + 1; 21 int right =run(root.right) + 1 ; 22 23 if(left == 1 || right == 1) 24 return left > right ? left : right; 25 else 26 return left > right ? right : left; 27 } 28 }
转载于:https://www.cnblogs.com/huangyichun/p/6783494.html
LeetCode | Minimum Depth of Binary Tree相关推荐
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
- LeetCode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...
- leetcode --Minimum Depth of Binary Tree
找最短的到叶节点的长度: 一考虑广度优先搜索(使用队列,不用 recursive) class Solution { public:int minDepth(TreeNode* root) {if(! ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- LeetCode: 111. Minimum Depth of Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
最新文章
- 在C++中对字符串std::string使用switch/case语句
- 页面异常反dump 及 内存访问异常hook
- 【Python】Flask 框架安装虚拟环境报错—处理中......
- android 一个activity定时更新另一个activity的UI
- linux是32还是64位,如何看linux是32位还是64位
- URL重写:RewriteCond指令与RewriteRule 指令格式
- mysql存储过程自定义结构体_(转)MySQL存储过程/存储过程与自定义函数的区别...
- mysql php错误处理函数_PHP 错误处理
- html调整图片之间的距离,html中如何调整图片之间的间距
- nginx跨域配置步骤
- mysql 导出数据 insert_mysql导出数据和导入数据
- vmware虚拟机使用多显示器
- 56: Recv failure: Connection was reset和55错误解决办法
- Windows不重启使用最新hosts文件
- Vulkan教程翻译之六 创建 Swapchain
- ubuntu下安装goldendict及离线词库
- 关于智能机器人的一些伦理道德问题
- AW笔记本升级SSD,外接双屏中的一些注意事项
- 如何让微信号开通检测软件替你顶起一片天?
- 人脸识别:路在何方?| 爱莫受邀参加VALSE Webinar报告会
热门文章
- Notepad++添加右键菜单
- plsql programming 18 包
- 大厂php怎么做前端,大厂前端经典面试问题精选(附答案)
- dataset for person re-id
- 图像语义分割模型DeepLab训练Cityscapes数据集过程记录
- TensorFlow patch块划分(transpose and reshape)
- 在数组中找到第 k 小的数
- 计算机网络英语求职简历翻译,计算机网络求职英文简历模板.doc
- 元素垂直水平居中布局
- 实验10:创建带有生命周期方法的bean ||实验11:测试bean的后置处理器