11#链接
2- -------
2+ -------
33> 牛客OJ:[ 二叉树的深度] ( http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&tqId=11191&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking )
4- >
4+ >
55> 九度OJ:http://ac.jobdu.com/problem.php?pid=1350
6- >
6+ >
77> GitHub代码: [ 039-二叉树的深度] ( https://github.com/gatieme/CodingInterviews/tree/master/039-二叉树的深度 )
88>
99> CSDN题解:[ 剑指Offer--039-二叉树的深度] ( http://blog.csdn.net/gatieme/article/details/51339884 )
1010
1111
12- | 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
12+ | 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 |
1313| ------------- | :-------------:| -----:|
1414| [ 039-二叉树的深度] ( http://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&tqId=11191&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking ) | [ 1350-二叉树的深度] ( http://ac.jobdu.com/problem.php?pid=1350 ) | [ 剑指Offer--039-二叉树的深度] ( http://blog.csdn.net/gatieme/article/details/51339884 ) | [ 039-二叉树的深度] ( https://github.com/gatieme/CodingInterviews/tree/master/039-二叉树的深度 ) |
1515
2222>
2323> 从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
2424
25+
26+ <br >** 您也可以选择[ 回到目录-剑指Offer--题集目录索引] ( http://blog.csdn.net/gatieme/article/details/51916802 ) **
27+
28+
29+
30+
31+
2532#分析
2633-------
34+
2735对二叉树进行层次遍历,维护一个层数计数器,每次进入一层就增加1,从而得到二叉树的层数。
2836当然如果使用递归的话,思路就更简单了,返回左右子树中深度最大的那个
2937
@@ -40,7 +48,7 @@ public:
4048 {
4149 return TreeDepth(root, 0);
4250 }
43-
51+
4452 /// 递归方法一
4553 int TreeDepthRecursion(TreeNode *root)
4654 {
@@ -115,7 +123,7 @@ public:
115123 int TreeDepth(TreeNode* root)
116124 {
117125 return TreeDepthRecursion(root);
118- //return TreeDepthRecursion(root, 0);
126+ //return TreeDepthRecursion(root, 0);
119127 //return LevelOrderDev(tree);
120128 //return LevelOrderUseEnd(tree);
121129 //return LevelOrderUseSize(tree);
@@ -199,7 +207,7 @@ public:
199207 int cur = 0;
200208 int end = 1;
201209 int count = 0;
202-
210+
203211 while (cur < vec.size())
204212 {
205213 end = vec.size(); /// 新的一行访问开始,重新定位last于当前行最后一个节点的下一个位置
@@ -229,7 +237,7 @@ public:
229237 int LevelOrderUseSize(TreeNode *root)
230238 {
231239 int count = 0;
232-
240+
233241 int parentSize = 1, childSize = 0;
234242 TreeNode *temp = NULL;
235243
@@ -311,9 +319,9 @@ int __tmain( )
311319{
312320// 0
313321// 1 2
314- // 3
322+ // 3
315323 TreeNode tree[ 4] ;
316-
324+
317325 tree[0].val = 0;
318326 tree[0].left = &tree[1];
319327 tree[0].right = &tree[2];
@@ -330,18 +338,18 @@ int __tmain( )
330338 tree[3].val = 3;
331339 tree[3].left = NULL;
332340 tree[3].right = NULL;
333-
341+
334342 Solution solu;
335343 cout <<solu.TreeDepth(tree) <<endl;
336-
344+
337345 cout <<solu.LevelOrderDev(tree) <<endl;
338346
339347 cout <<solu.LevelOrderUseEnd(tree) <<endl;
340-
348+
341349 cout <<solu.LevelOrderUseSize(tree) <<endl;
342350
343351 cout <<solu.LevelOrderUsePoint(tree) <<endl;
344-
352+
345353 return 0;
346354}
347- ```
355+ ```
0 commit comments