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

题目描述

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

例如:
给定二叉树 [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

返回其自底向上的层次遍历为:

[
[15,7],
[9,20],
[3]
]

题目分析

  依然是二叉树的层次遍历,采取BFS算法,最后的逆序只是一个小插曲而已。

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>> ans = new ArrayList<>();
List<Integer> line = new ArrayList<>(); if(root == null)
return ans;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root); line.add(root.val);
ans.add(line);
line = new ArrayList<>(); int size = queue.size();
int flag = 1;
while(!queue.isEmpty())
{
TreeNode tmp = queue.poll();
if(tmp.left!=null)
{
queue.add(tmp.left);
line.add(tmp.left.val);
}
if(tmp.right!=null)
{
queue.add(tmp.right);
line.add(tmp.right.val);
} size--;
if(size==0&&line.size()>0)
{
size=queue.size();
ans.add(line);
line = new ArrayList<>();
}
}
Collections.reverse(ans);
return ans;
}
}

  

LeetCode:二叉树的层次遍历||【107】的更多相关文章

  1. (leetcode)二叉树的层次遍历-c语言实现

    这段代码,在后面跑测试用例时,出现了stack-overflow,但是原因还不清楚. 问题如下:  二叉树的层次遍历   给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点) ...

  2. LeetCode 二叉树的层次遍历

    第102题 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

  3. LeetCode 二叉树的层次遍历 C++

    给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层 ...

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

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

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

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

  6. 【leetcode-102,107,103】 二叉树的层次遍历

    102. 二叉树的层次遍历 (1过,隐蔽错误花时间很多,简单题目本应很快,下次注意红色错误的地方) 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: ...

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

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

  8. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  9. LeetCode 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. ulimit调优|设置普通用户的ulimit值

    个人总结: 如何设置普通用户的ulimit值 1.vim /etc/profile 增加 ulimit -n 10240 source /etc/profile 重新启动就不需要运行这个命令了. 2. ...

  2. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  3. 游戏引擎 Unity 的入门易精通难体现在哪?为什么?

    04月212014年   [王楠的回答(37票)]: 为什么入门简单,看一下官网的文档和视频教程就知道了,看完几段视频和例子就能让初学者做出能玩的东西,其他同类商业引擎都做不到.物体+组件的结构,所见 ...

  4. cf #363 d

      time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan ...

  5. linux虚拟文件系统vfs

    linux可以挂载不同的文件系统(EXT2,FAT,NTFS),用同一的样式呈现给用户,读写操作用起来都一样,这是怎样做到的呢? linux内核在各种不同的文件系统格式上做了一个抽象层,使得文件.目录 ...

  6. 配置LANMP环境(5)-- 安装NGINX与配置

    安装nginx yum install nginx 若提示找不到nginx,则在软件源中添加nginx的软件源文件: vim /etc/yum.repos.d/nginx.repo 添加如下内容: [ ...

  7. 使用sqoop1.4.4从oracle导入数据到hive中错误记录及解决方案

    在使用命令导数据过程中,出现如下错误 sqoop import --hive-import --connect jdbc:oracle:thin:@192.168.29.16:1521/testdb ...

  8. vue之v-bind:style

    <div class="collect" @click="collected=!collected"> <i class="fa f ...

  9. EasyNVR无插件直播流媒体服务器云端集中管控的EasyNVS云管理平台安装使用文档

    EasyNVS - EasyNVR云端集中管理服务 EasyNVS云管理平台是一套专门用于集中化管理EasyNVR 的解决方案. EasyNVR 采用主动注册的方式接入到 EasyNVS, 再由 Ea ...

  10. MogoDB 分片键

    MongoDB 根据分片键分割 collection 中的文档,然后分配到分片集群的成员中. 分片键可以是一个存在于每个文件中的索引字段或者复合索引字段. MongoDB 使用不同范围的分片键值来分割 ...