[LeetCode] 107. 二叉树的层次遍历 II
题目链接 : https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
题目描述:
给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其自底向上的层次遍历为:
[
[15,7],
[9,20],
[3]
]
思路:
与上一题层次遍历一样,只不过输出的顺序取反了!
所以只需要从头添加数组就可以了!
思路一: 迭代
思路二: 递归
代码:
思路一:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
from collections import deque
if not root: return []
queue = deque()
queue.appendleft(root)
res = []
while queue:
tmp = []
n = len(queue)
for _ in range(n):
node = queue.pop()
tmp.append(node.val)
if node.left:
queue.appendleft(node.left)
if node.right:
queue.appendleft(node.right)
res.insert(0, tmp)
return res
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new LinkedList<>();
if (root == null) return res;
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
List<Integer> tmp = new ArrayList<>();
int n = queue.size();
for (int i = 0; i < n; i++) {
TreeNode node = queue.poll();
tmp.add(node.val);
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
res.add(0, tmp);
}
return res;
}
}
思路二:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
res = []
def helper(root, depth):
if not root: return
if depth == len(res):
res.insert(0, [])
res[-(depth+1)].append(root.val)
helper(root.left, depth+1)
helper(root.right, depth+1)
helper(root, 0)
return res
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root){
List<List<Integer>> res = new LinkedList<>();
helper(res, root, 0);
return res;
}
private void helper(List<List<Integer>> res, TreeNode root, int depth) {
if (root == null) return;
if (res.size() == depth) res.add(0, new ArrayList<>());
res.get(res.size() - depth - 1).add(root.val);
helper(res, root.left, depth + 1);
helper(res, root.right, depth + 1);
}
}
[LeetCode] 107. 二叉树的层次遍历 II的更多相关文章
- Java实现 LeetCode 107 二叉树的层次遍历 II(二)
107. 二叉树的层次遍历 II 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null, ...
- LeetCode 107 ——二叉树的层次遍历 II
1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...
- 107. 二叉树的层次遍历 II
107. 二叉树的层次遍历 II 题意 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历). 解题思路 递归:利用前序遍历的思想,在递归过程中 ...
- LeetCode107. 二叉树的层次遍历 II
107. 二叉树的层次遍历 II 描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例 例如,给定二叉树: [3,9,20,null ...
- LeetCode:二叉树的层次遍历||【107】
LeetCode:二叉树的层次遍历||[107] 题目描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,2 ...
- 刷题-力扣-107. 二叉树的层序遍历 II
107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...
- lintcode : 二叉树的层次遍历II
题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...
- LintCode 二叉树的层次遍历 II
中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...
- LintCode-70.二叉树的层次遍历 II
二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 按照 ...
随机推荐
- Linux下lazarus交叉编译 win32[win64]
环境 vmvare + deepin Linux64 + lazarus2.0.6 参考:https://wiki.freepascal.org/Cross_compiling_for_Win32_u ...
- sqlserver 返回刚插入的那条数据
insert into xxxxxx(Col_002,UrgentStatus,DoWorkShop,Col_004,Col_005,Col_006,Col_003,Col_007,postQQ,Co ...
- 使用WireMock伪造REST服务
在真正的rest api服务还没有写好之前,为了方便前端测试调用,后端可以写个服务,伪造rest服务(写假数据) 1.官网: http://wiremock.org/ 下载可执行jar:http:// ...
- DNS预读取 dns-prefetch 提升页面载入速度
DNS Prefetch,即DNS预获取,是前端优化的一部分.一般来说,在前端优化中与 DNS 有关的有两点: 一个是减少DNS的请求次数,另一个就是进行DNS预获取 . DNS 作为互联网的基础协议 ...
- UVa 1602 Lattice Animals (STL && 生成n连块 && 无方向形状判重)
题意 : 给定一个 w * h 的 矩阵,在矩阵中找不同n个连通块的个数(旋转,翻转,平移算作一种) 分析 : 这题的关键点有两个 ① 生成n连块并且存储起来(因为题目是多测试用例,如果每一次都重新生 ...
- POJ 3180 牛围着池塘跳舞 强连通分量裸题
题意:一群牛被有向的绳子拴起来,如果有一些牛(>=2)的绳子是同向的,他们就能跳跃.求能够跳跃的组数. #include <iostream> #include <cstdio ...
- ApiException
ApiException 用于在catch中throw 异常,可以添加异常信息, throw new ApiException("线下绑卡异常!"); public class A ...
- 【错误记录】windows python 路径中的一个转义错误:'rawunicodeescape' codec can't decode bytes in position 112-113: truncated \uXXXX
ur"D:\work\结构化\CSV\useful\内容.csv" 报错 编码错误原因,当路径中有\u这种字串时,即使是包含在r"" 中也会进行转义,然后转义出 ...
- Json C#解析
介绍 项目中数据格式如果是是Json格式,推荐大家使用LitJson和Newtonsoft.json进行解析 库的详细介绍和下载地址 推荐使用VS自带的Nuget来使用 Newtonsoft.Json ...
- js实现两个从input获取到的数字相加引发的问题
从input中获取到的数据是文本类型的,如果不转化类型直接相加会变成字符串的相加. 使用Number()函数可以解决这个问题,如下 var c = Number(a) + Number(b)