Leetcode_104_Maximum Depth of Binary Tree
本文是在学习中的总结,欢迎转载但请注明出处: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)为了求得二叉树最大深度,本文也是考虑运用二叉树按层次遍历的思想,二叉树遍历层次的次数即为二叉树的最大深度,这里很容易理解。
(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; }
Leetcode_104_Maximum Depth of Binary Tree的更多相关文章
- [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] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [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 ...
- [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 ...
- 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 —— 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 ...
- 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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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 ...
随机推荐
- RHEL Linux常用指令
查询已安装软件包 rpm -qa|grep * 安装软件 rpm -ivh * 查询Linux版本 uname -a lsb_release -a cat /etc/redhat-release ca ...
- ACM Doing Homework again
Ignatius刚刚从第30届ACM / ICPC回到学校.现在他有很多作业要做.每个老师给他一个截止作业的截止日期.如果Ignatius在截止日期之后进行了家庭作业,老师将减少他的最终考试成绩.现在 ...
- jQuery 遍历 – 后代
后代是子.孙.曾孙等等. 通过 jQuery,您能够向下遍历 DOM 树,以查找元素的后代. 向下遍历 DOM 树 下面是两个用于向下遍历 DOM 树的 jQuery 方法: children() f ...
- Java 求n天前的时间或者n月前的时间
时间格式化 public static String DEFAULT_FORMATDATE = "yyyy-MM-dd"; 1.n天前的日期 /** * luyanlong * 默 ...
- 【java集合系列】---HashSet
在前面的博文中,小编主要简单介绍了java集合中的总体框架,以及list接口中典型的集合ArrayList和LinkedList,接着,我们来看set的部分集合,set集合和数学意义上的集合没有差别, ...
- API得到Windows版本
API得到Windows版本 /** * Windows Version * https://msdn.microsoft.com/en-us/library/windows/desktop/dn48 ...
- Python logging 模块和使用经验
记录下常用的一些东西,每次用总是查文档有点小麻烦. py2.7 日志应该是生产应用的重要生命线,谁都不应该掉以轻心 有益原则 级别分离 日志系统通常有下面几种级别,看情况是使用 FATAL - 导致程 ...
- shell编程--流程控制for,do-while,if-then,break,continue,case等
2.5 流程控制 2.5.1 if语法 1.语法格式 if condition then statements [elif condition then statements. ..] ...
- openresty 备忘
The problem with: apt-get --yes install $something is that it will ask for a manual confirmation if ...
- 物料REVISION控制
--新增 INV_ITEM_REVISION_PUB.Create_Item_Revision ( p_api_version IN NUMBER , p_init_msg_list IN VARCH ...