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 ...
随机推荐
- LNMP 多版本PHP同时运行
首先需要装好两个版本以上的PHP(例如:php5.6和php7两个版本).这里假设你已安装完成.1.配置并启动php默认版本: (设置 nginx 的 vhost 域名配置文件监听端口就好) 1).打 ...
- Python之面向对象之反射、内置方法
一.静态方法(staticmethod)和类方法(classmethod) 类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属性交互(也就是可以使用类属性) 静态方法:让类里的方法直接被 ...
- JVM垃圾回收之CMS收集器
从前文JVM垃圾回收几种常见算法和常见收集器我们知道,CMS是老年代垃圾收集器.CMS 收集器主要关注系统停顿时间.CMS 是 Concurrent Mark Sweep 的缩写,意为并发标记清除,从 ...
- ps制作雾的效果
制作雾的效果 方法一: 新建图层,将前景色设置为白色,背景色为黑色(因为雾的颜色是根据前景色决定的,也可根据这个原理制作火焰效果) 选择滤镜->渲染->云彩(也可选择其他效果的云彩) (选 ...
- jackson 完整Jar包
Jackson fasterxml和codehaus的区别: 他们是Jackson的两大分支.也是两个版本的不同包名.Jackson从2.0开始改用新的包名fasterxml: 1.x版本的包名是co ...
- linux-awk-3
awk 基础语法 Awk –Fs '/pattern/ {action}' input-file (或者) Awk –Fs '{action}' input-file -F 为字段分界符.如果不指定, ...
- UVa 1596 Bug Hunt (string::find && map && 模拟)
题意 : 给出几组由数组定义与赋值构成的编程语句, 有可能有两种BUG, 第一种为数组下标越界, 第二种为使用尚未定义的数组元素, 叫你找出最早出现BUG的一行并输出, 每组以' . '号分隔, 当有 ...
- 51Nod 1413 权势二进制 (思维)
题意 : 一个十进制整数被叫做权势二进制, 当他的十进制表示的时候只由0或1组成.例如0, 1, 101, 110011都是权势二进制而2, 12, 900不是.当给定一个n (1<=n< ...
- eclipse导出java项目jar包(依赖第三方jar包)
一.在项目根目录下建一个文件:MANIFEST.MF 内容: Manifest-Version: 1.0 Class-Path: lib/commons-compress-1.9.jar lib/co ...
- 当遇到npm ERR! Unexpected end of JSON input while parsing near……时的解决办法
运行npm install时有时会遇到以下错误: npm ERR! Unexpected end of JSON input while parsing near ... 这时可以先执行下面的命令: ...