Maximum Depth of Binary Tree 解答
Question
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.
Solution 1 -- Recursion
Recursion way is easy to think. Similar with "Minimum Depth of Binary Tree", time complexity is O(n).
/**
* 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 maxDepth(TreeNode root) {
if (root == null)
return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
Solution 2 -- Iteration
BFS: We can still use level order traversal to get depth.
/**
* 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 maxDepth(TreeNode root) {
if (root == null)
return 0;
List<TreeNode> current = new ArrayList<TreeNode>();
current.add(root);
List<TreeNode> next;
int result = 1;
while (current.size() > 0) {
next = new ArrayList<TreeNode>();
for (TreeNode tmpNode : current) {
if (tmpNode.left != null)
next.add(tmpNode.left);
if (tmpNode.right != null)
next.add(tmpNode.right);
}
current = next;
result++;
}
return result - 1;
}
}
DFS: Traverse tree while recording level number.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
node_stack = [root]
value_stack = [1]
result = 1
while node_stack:
node = node_stack.pop()
value = value_stack.pop()
result = max(value, result)
if node.left:
node_stack.append(node.left)
value_stack.append(value + 1)
if node.right:
node_stack.append(node.right)
value_stack.append(value + 1)
return result
Maximum Depth of Binary Tree 解答的更多相关文章
- [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 | 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 ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- SQL省市区三级表结构
-- 表的结构 areaDROP TABLE area;CREATE TABLE area ( id int NOT NULL , areaID int NOT NULL, area va ...
- seajs教程之seajs学习笔记 seajs.use用法
seajs.use 用来在页面中加载模块.通过 use 方法,可以在页面中加载任意模块. 实例地址:http://www.android100.org/html/201405/23/12807.htm ...
- Encode and Decode Strings 解答
Question Design an algorithm to encode a list of strings to a string. The encoded string is then sen ...
- thinkphp实现excel数据的导入导出
下载地址:phpexcel.rar 实现步骤: 一:在http://phpexcel.codeplex.com/下载最新PHPExcel放到Vendor下,注意位置:ThinkPHP\Extend\V ...
- Java里多个Map的性能比較(TreeMap、HashMap、ConcurrentSkipListMap)
问题 比較Java原生的 1. TreeMap 2. HashMap 3. ConcurrentSkipListMap 3种Map的效率. 结果 模拟150W以内海量数据的插入和查找,通过添加和 ...
- iOS ARC注释和错误的解决方法在使用
1.一个错误The current deployment target does not support automated __weak references 这个错误被所述支持iOS版本号不支持相 ...
- MSN在Win7下80072f0d错误解决
近期电脑(笔记本联想 K41A)显卡出了点问题(该显卡一周前刚换的新的,竟然不到一周又出问题了,联想的质量真的...),在xp下电脑根本进不了操作系统,不断重新启动(可能驱动.系统垃圾太多有关),于是 ...
- boost库在工作(39)网络UDP异步服务端之九
前面创建的UDP服务器和客户端,都是同步的方式,也就是说当接收数据时,不能参与别的事情执行的.如果在一个只有界面线程的程序里,又不想创建多线程,导致复杂程度的增加,在这种情况之下,我们还有一个方案可以 ...
- Chrome开发者工具详解(3):Timeline面板
Timeline面板 Timeline面板是整个面板里面最复杂的一个面板,涉及的东西比较多.可以利用这个面板来记录和分析网页运行过程中的所有活动行为信息. 你可以充分利用这个面板来分析你的网页的程序性 ...
- asp.net mvc输出自定义404等错误页面,非302跳转
朋友问到一个问题,如何输出自定义错误页面,不使用302跳转.当前页面地址不能改变. 还要执行一些代码等,生成一些错误信息,方便用户提交反馈. 500错误,mvc框架已经有现成解决方法: filte ...