Maximum Depth of Binary Tree 【leetCode】
2014-12-24 16:16
274 查看
基本上可以采用Minimum Depth of Binary Tree的代码,具体如下:
上一篇的连接如下:http://blog.csdn.net/wolinxuebin/article/details/42124719
目录:
1、题目及分析
1.1 题目
1.2 分析
2、实现
2.1 方法一
2.2 方法二
1、题目及分析
1.1 题目:
Maximum Depth of Binary Tree
1.2 分析
这道题目是一道很简单的基础题,通过遍历所有的路径就可以实现。
2、实现
2.1 方法一
通过使用队列的方法,一层层的进行遍历,在每一层的末尾处加入null,以显示一层的结束。见下图示意:
![](http://img.blog.csdn.net/20141224154901009?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29saW54dWViaW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](file:///C:/Users/lin/AppData/Local/YNote/data/linxuebin1990@126.com/8432dd2a8e2e492ba76ce33ad7f20e02/clipboard.png)
代码如下:
![](file:///C:/Users/lin/AppData/Local/YNote/data/linxuebin1990@126.com/d498b3d091234fdd80799bccceb32472/clipboard.png)
2.2 方法二
参考自:https://oj.leetcode.com/discuss/6308/my-solution-used-level-order-traversal
使用迭代的方式进行处理。代码如下:
个人感觉此代码的经典之处在于,如下代码
上一篇的连接如下:http://blog.csdn.net/wolinxuebin/article/details/42124719
目录:
1、题目及分析
1.1 题目
1.2 分析
2、实现
2.1 方法一
2.2 方法二
1、题目及分析
1.1 题目:
Maximum Depth of Binary Tree
1.2 分析
这道题目是一道很简单的基础题,通过遍历所有的路径就可以实现。
2、实现
2.1 方法一
通过使用队列的方法,一层层的进行遍历,在每一层的末尾处加入null,以显示一层的结束。见下图示意:
![](file:///C:/Users/lin/AppData/Local/YNote/data/linxuebin1990@126.com/8432dd2a8e2e492ba76ce33ad7f20e02/clipboard.png)
代码如下:
public int minDepth(TreeNode root) { //使用队列的方式 Queue<TreeNode> q = new LinkedList<TreeNode>(); TreeNode tmp; int depth = 0; if(null == root) return 0; q.offer(root); q.offer(null); depth ++; while(q.isEmpty() == false){ tmp = q.poll(); if(tmp != null){ //并联结构的,别写成if else了 if(null != tmp.left) //入队列 q.offer(tmp.left); if(null != tmp.right) //入队列 q.offer(tmp.right); } else{ <span style="white-space:pre"> if(q.isEmpty() == true)//如果为空,那么就是全部遍历完了 return depth;</span> q.offer(null); depth ++; } } return 0; }
![](file:///C:/Users/lin/AppData/Local/YNote/data/linxuebin1990@126.com/d498b3d091234fdd80799bccceb32472/clipboard.png)
2.2 方法二
参考自:https://oj.leetcode.com/discuss/6308/my-solution-used-level-order-traversal
使用迭代的方式进行处理。代码如下:
个人感觉此代码的经典之处在于,如下代码
if(left==0) //由于确定left为0,那么这个子root的长度就由right决定,再加上本身的1 return right+1;
public int minDepth(TreeNode root) {//采用递归的方式 if(root == null) return 0; int left=minDepth(root.left); int right=minDepth(root.right); //求的是leaf,只有一边为null的不是leaf if(left==0) //由于确定left为0,那么这个子root的长度就由right决定,再加上本身的1 return right+1; if(right==0) //由于确定right为0,那么这个子root的长度就由left决定,再加上本身的1 return left+1; return left>right ? left+1:right+1; //两天路径求取最小的 }
相关文章推荐
- [Leetcode 8] 104 Maximum Depth of Binary Tree
- 【LeetCode with Python】 Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- LeetCode_Maximum Depth of Binary Tree
- LeetCode题目笔记(二) -- Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- [leetcode] Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- [leetcode刷题系列]Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode: Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree
- LeetCode-Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree