Maximum Depth of Binary Tree(二叉树最大深度)
来源:https://leetcode.com/problems/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. 深度遍历,递归
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null) {
return 0;
}
int leftDepth = maxDepth(root.left) + 1;
int rightDepth = maxDepth(root.right) + 1;
return leftDepth > rightDepth ? leftDepth : rightDepth;
}
}
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
left_depth = self.maxDepth(root.left) + 1
right_depth = self.maxDepth(root.right) + 1
return left_depth if left_depth>right_depth else right_depth
2. 广度遍历(逐层遍历),使用队列
Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<TreeNode>();
int depth = -1;
queue.offer(root);
TreeNode node = null;
int queueSize = 0;
while(!queue.isEmpty()) {
queueSize = queue.size();
while(queueSize-- > 0) {
node = queue.poll();
if(node == null) {
continue;
}
queue.offer(node.left);
queue.offer(node.right);
}
depth += 1;
}
return depth;
}
}
Python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
queue = [root]
depth = -1
while queue:
depth += 1
queue = [kid for node in queue if node for kid in (node.left, node.right)]
return depth
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 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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 ...
- [LeetCode] 104. 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] 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] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)
描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- Maximum Depth of Binary Tree 二叉树的深度
Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longe ...
随机推荐
- jquery预览本地图片
本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加). QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29Lo ...
- DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...
- 5G技术被夸大?专家来测试一下
像大多数新技术一样,5G也带来了大量媒体宣传.这种炒作中有些伴随着事实的严重扭曲和5G技术实际功能的放大.但是,有一个普遍共识的说法是5G将实现“极速”,换句话说,与前几代产品相比,带宽要高得多. 这 ...
- 【NOIP2016提高A组模拟8.14】总结
第一题是几何题,没去想直接弃疗.... 第二题觉得很像背包,但是单挑人的顺序不同,答案也会不同,我比较了每个人先后的优劣性,成功搞定了这道题.但是再输出时不小心搞错了,爆零. 第三题,我答案了整整一个 ...
- Android图片缩放 指定尺寸
//使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) { ...
- Spring Boot教程(四)接收上传的multi-file的文件
构建工程 为例创建一个springmvc工程你需要spring-boot-starter-thymeleaf和 spring-boot-starter-web的起步依赖.为例能够上传文件在服务器,你需 ...
- 【商业智能VS人工智能】
什么是智能? 从感觉到记忆到思维这一过程,称为“智慧”,智慧的结果就产生了行为和语言,将行为和语言的表达过程称为“能力”,两者合称“智能”,将感觉.去记.回忆.思维.语言.行为的整个过程称为智能过程, ...
- SQL Server新老版本CE区别
对比CE7和2014 CE12的区别: 1.表连接中连接列估算方式 老CE对所有参与连接列的统计信息step进行逐个估算.新CE只对于最大和最小step统计信息进行收集估算,在连接列的值分布不均匀的时 ...
- Mac ssh key生成
转载https://blog.csdn.net/wangjunling888/article/details/51115659 1. 查看秘钥是否存在 打开终端查看是否已经存在SSH密钥:cd ~/. ...
- __proto__ VS. prototype in JavaScript
__proto__ VS. prototype in JavaScript http://dmitrysoshnikov.com/ecmascript/javascript-the-core/#a-p ...