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 ...
随机推荐
- JavaScript - 基于原型的面向对象
JavaScript - 基于原型的面向对象 1. 引言 JavaScript 是一种基于原型的面向对象语言,而不是基于类的!!! 基于类的面向对象语言,比如 Java,是构建在两个不同实体的概念之上 ...
- onethink 验证码二维码不显示的问题
常规思路: 1 检查GD和FreeType.在项目根目录(index.php同级)下放一个php文件 <?php echo phpinfo(); ?> 访问此文件,查看GD和FreeTyp ...
- J2EE之普通类载入web资源文件的方法
在WEB中普通类并不能像Servlet那样通过this.getServletContext().getResourceAsStream()获取web资源,须要通过类载入器载入,这里有两种方式,这两种方 ...
- menu控件绑定sql数据库
public int treeID; protected void Page_Load(object sender, EventArgs e) { SqlConnection CN = new Sql ...
- IE浏览器中hasLayout的介绍
haslayout是Windows Internet Explorer渲染引擎的一个内部组成部分.在InternetExplorer中,一个元素要么对自身的内容进行计算大小和组织,要么依赖于父元素来计 ...
- JavaScript ----------------- 寄生式继承
寄生式继承 寄生式继承是于原型式继承紧密相关的一种思路.寄生式基础的思路与寄生构造函数和工厂模式类似,既创建一个仅用于封装继承过程的函数,该函数内部以某种方式来增强对象,最后再像真地是它做了所有工作一 ...
- iOS 面试题 2
1. 描述应用程序的启动顺序. 1.程序入口main函数创建UIApplication实例和UIApplication代理实例 2.在UIApplication代理实例中重写启动方法, ...
- vs2013调试的时候卡顿
做毕业设计的时候忽然发现开始调试以后 会卡顿,这在前两天是没有的,纳尼,把我愁坏了. 首先以为程序或者vs出问题了.随后发现每次调试以后cpu急剧增加.随后就开始卡顿. 随后去网上搜索,发现两个答案. ...
- (转)android ndk 给结构体赋值的方法
转自:http://www.cnweblog.com/fly2700/archive/2012/03/21/320083.html 1,java 代码 结构体定义 public class Media ...
- Java中的IO学习总结
今天刚刚看完java的io流操作,把主要的脉络看了一遍,不能保证以后使用时都能得心应手,但是最起码用到时知道有这么一个功能可以实现,下面对学习进行一下简单的总结: IO流主要用于硬盘.内存.键盘等处理 ...