Minimum Depth of Binary Tree
来源:互联网 发布:耶路撒冷 知乎 编辑:程序博客网 时间:2024/05/21 09:42
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
注意,左边为空的时候,只返回右边的+1,右边为空的时候,只返回左边的+1,只有两边都不为空的时候,才返回两者的最小值+1;
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null && root.right == null) return 1; int leftdepth = minDepth(root.left); int rightdepth = minDepth(root.right); if(leftdepth == 0) return rightdepth+1; if(rightdepth == 0) return leftdepth+1; return Math.min(leftdepth, rightdepth)+1; }}
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- nvr向sip服务器注册失败(未携带摘要等类信息)
- python3.4 访问 oracle11g
- ubuntu14部署ceph10.2版本
- 如何在自定义view确定你点击的位置位于哪个小的子视图
- Linux 中的防火墙 端口记录
- Minimum Depth of Binary Tree
- 安tomcat在linux上
- iOS学习项目(11-QQ聊天)
- Apache 本地配置多个虚拟站点
- EasyDarwin接入ffmpeg实现264转图片快照功能
- java网络编程:RandomAccessFile, URLConnection和多线程机制实现了Http下载
- 图广搜。。深搜自行改编。。。
- 他山之石之用RecyclerView来展示图片,替换GridView和Gallery
- CSS3圆角圆边 支持IE6-IE8浏览器