本文研究的总结,欢迎转载,但请注明出处:http://blog.csdn.net/pistolove/article/details/41964475

Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

思路:

(1)题意为找到二叉树最大深度:即为从树根到叶子节点的最大路径。

(2)该题思路和遍历二叉树和寻找二叉树最短深度类似,能够參照二叉树按层次遍历实现http://blog.csdn.net/pistolove/article/details/41929059

(3)为了求得二叉树最大深度。本文也是考虑运用二叉树按层次遍历的思想,二叉树遍历层次的次数即为二叉树的最大深度,这里非常easy理解。

(4)基本的解题思路还是二叉树按层次遍历。本文仅仅只是顺手把其拿过来使用罢了。希望对你有所帮助。

谢谢。

算法代码实现例如以下所看到的:

//最大深度
public static int getDeep(TreeNode root){
if(root==null) return 0;
int level = 0;
LinkedList<TreeNode> list = new LinkedList<TreeNode>();
list.add(root);
int first = 0;
int last = 1;
while(first<list.size()){
last = list.size();
while(first<last){
if(list.get(first).left!=null){
list.add(list.get(first).left);
}
if(list.get(first).right!=null){
list.add(list.get(first).right);
}
first++;
}
level++;
}
return level;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Lettcode_104_Maximum Depth of Binary Tree的更多相关文章

  1. [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 ...

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  3. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  5. 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 ...

  6. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  8. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. JS中的发布订阅模式

    一. 你是如何理解发布订阅模式的 JS中的设计模式: 单例模式:处理业务逻辑 构造原型模式:封装类库,组件,框架,插件等 类库:jQuery 只是提供了一些常用的方法,可以应用到任何的项目中,不具备业 ...

  2. 如何在本地运行查看github上的开源项目

    看中了一款很多星星的github的项目,想把这个项目拉到自己的电脑上运行查看项目效果,该怎么做?示例:我们今天要看的 github项目地址:https://github.com/lzxb/vue-cn ...

  3. 【 2017 Multi-University Training Contest - Team 9 && hdu 6162】Ch’s gift

    [链接]h在这里写链接 [题意] 给你一棵树,每个节点上都有一个权值. 然后给你m个询问,每个询问(x,y,a,b); 表示询问x->y这条路径上权值在[a,b]范围内的节点的权值和. [题解] ...

  4. 【Codeforces Round #433 (Div. 2) C】Planning

    [链接]h在这里写链接 [题意] 让你确定ti,使得∑(ti-i)*gi最小,其中ti∈[k+1..k+n],且每个ti都不能一样. 且ti>=i必须成立. [题解] 分解一下成为∑ti*gi ...

  5. HDU 1556 Color the ball【算法的优化】

    /* 解题思路:每次仅仅求解一開始的第一个数字,让第一个数字加一,最后的一个数字的后面一个数减一.我们能够想想,最后加的时候,就是加上前面一个数出现的次数和自己本身出现的次数. 解题人:lingnic ...

  6. codeforces Round 246 D. Prefixes and Suffixes (后缀数组 || KMP)

    题目大意: 求一个子串,子串既是前缀又是后缀. 然后再求出它在整个串中出现的次数. 思路分析: 能够非常easy想到怎样推断一个串既是前缀又是后缀. 仅仅须要它与 sa[0] 的lcp 等于 整个串的 ...

  7. CloudFoundry hm9000原理及排错

    hm9000跟hm_next(healthmanager)功能类似.在cloudfoundry集群中担任至关重要的角色 - 尝试启动缺失情况下的实例,停止异常实例 - 获知和报告应用执行的实际实例个数 ...

  8. 内存、时间复杂度、CPU/GPU以及运行时间

    衡量 CPU 的计算能力: 比如一个 Intel 的 i5-2520M @2.5 Ghz 的处理器, 则其计算能力 2.5 * 4(4核) = 10 GFLOPS FLOP/s,Floating-po ...

  9. vue2.0 踩坑记录之组件

    - did you register the component correctly? For recursive components, make sure to provide the " ...

  10. Vue源码--深入模板渲染

    原文链接:https://geniuspeng.github.io/2018/02/07/vue-compile/ 之前整理了vue的响应式原理,在这里有一点是一直很模糊的,就是何时去new一个wat ...