题目链接 : 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的更多相关文章

  1. Java实现 LeetCode 107 二叉树的层次遍历 II(二)

    107. 二叉树的层次遍历 II 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 给定二叉树 [3,9,20,null,null, ...

  2. LeetCode 107 ——二叉树的层次遍历 II

    1. 题目 2. 解答 与 LeetCode 102 --二叉树的层次遍历 类似,我们只需要将每一层的数据倒序输出即可. 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level ...

  3. 107. 二叉树的层次遍历 II

    107. 二叉树的层次遍历 II 题意 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历). 解题思路 递归:利用前序遍历的思想,在递归过程中 ...

  4. LeetCode107. 二叉树的层次遍历 II

    107. 二叉树的层次遍历 II 描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例 例如,给定二叉树: [3,9,20,null ...

  5. LeetCode:二叉树的层次遍历||【107】

    LeetCode:二叉树的层次遍历||[107] 题目描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如:给定二叉树 [3,9,2 ...

  6. 刷题-力扣-107. 二叉树的层序遍历 II

    107. 二叉树的层序遍历 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-level-order-tr ...

  7. lintcode : 二叉树的层次遍历II

    题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, ...

  8. LintCode 二叉树的层次遍历 II

    中等 二叉树的层次遍历 II 查看执行结果 42% 通过 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 您在真实的面试中是否遇到过这个 ...

  9. LintCode-70.二叉树的层次遍历 II

    二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 按照 ...

随机推荐

  1. 利用aspose-words直接将Word转化为图片

    之前遇到一个需求,需要在word文档中加入一些文字,并转化为图片.之前也试过几种方案,但是发现效果还不是很理想,且中间需要经过一次转化为pdf的过程,最近找到了最理想的方式,即利用aspose-wor ...

  2. HTTS TTLS 433

    HTTP和HTTPS协议,看一篇就够了 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/x ...

  3. Python 爬虫十六式 - 第二式:urllib 与 urllib3

    Python请求标准库 urllib 与 urllib3 学习一时爽,一直学习一直爽!   大家好,我是 Connor,一个从无到有的技术小白.上一次我们说到了什么是HTTP协议,那么这一次我们就要动 ...

  4. jquery empty选择器 语法

    jquery empty选择器 语法 作用::empty 选择器选取空的元素.空元素指的是不包含子元素或文本的元素.直线电机滑台 语法:$(":empty") jquery emp ...

  5. AJAX - 服务器 响应

    AJAX - 服务器 响应 服务器响应 如需获得来自服务器的响应,请使用 XMLHttpRequest 对象的 responseText 或 responseXML 属性.大理石构件来图加工 属性 描 ...

  6. android 文件保存到应用和sd卡中

    <span style="font-size:18px;">1.权限添加 <uses-permission android:name="android. ...

  7. sql中left join、right join、inner join的区别

    转自https://www.cnblogs.com/pcjim/articles/799302.html left  join.right join.inner join区别 left join(左联 ...

  8. [BZOJ3990]:[SDOI2015]排序(搜索)

    题目传送门 题目描述 小A有一个1-${2}^{N}$的排列A[1..${2}^{N}$],他希望将A数组从小到大排序,小A可以执行的操作有N种,每种操作最多可以执行一次,对于所有的i(1≤i≤N), ...

  9. 说下Java堆空间结构,及常用的jvm内存分析命令和工具

    Java堆空间结构图:http://www.cnblogs.com/SaraMoring/p/5713732.html JVM内存状况查看方法和分析工具: http://blog.csdn.net/n ...

  10. springboot 配置访问本地图片

    spring.mvc.static-path-pattern=/image/** spring.resources.static-locations=file:D://image/