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. Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Errcode: 17)

    这个错误时在Mysql主从配置产生的,最后找到这个Mysql的一个bug http://bugs.mysql.com/bug.php?id=62055 bug的主要原因是:打开文件的函数中指定打开模式 ...

  2. collection动画

    Collection View 动画 吴迪 12 May 2014 分享文章 UICollectionView 和相关类的设置非常灵活和强大.但是灵活性一旦增强,某种程度上也增加了其复杂性: UICo ...

  3. openWRT自学---针对backfire版本的主要目录和文件的作用的分析整理

    特别说明:要编译backfire版本,一定要通过svn下载:svn co svn://svn.openwrt.org/openwrt/branches/backfire,而不能使用http://dow ...

  4. input子系统分析(转)

    转自:http://www.linuxidc.com/Linux/2011-09/43187.htm 作者:作者:YAOZHENGUO2006 Input子系统处理输入事务,任何输入设备的驱动程序都可 ...

  5. Pairs Forming LCM 在a,b中(a,b<=n)(1 ≤ n ≤ 10^14),有多少组(a,b) (a<b)满足lcm(a,b)==n; lcm(a,b)=p1 ^ max(a1,b1) * p2 ^ max(a2,b2) *..........*pn ^ max(an,bn)

    转自:http://www.cnblogs.com/shentr/p/5285407.html http://acm.hust.edu.cn/vjudge/contest/view.action?ci ...

  6. docker导入导出

    导出镜像 docker save -o centos7.tar centos # 导入本地镜像 docker load --input centos7.tar docker ps -a docker ...

  7. CentOS 6.5 Git源码安装

    首先清除系统自带git,使用如下命令 yum -y remove git 一.下载Git源码包 wget https://www.kernel.org/pub/software/scm/git/git ...

  8. yii rule

    https://blog.csdn.net/ljfrocky/article/details/46373691 http://www.yiichina.com/tutorial/997 http:// ...

  9. 4、easyUI-七种布局(layout)

    1.为网页创建边框布局 边框布局(border layout)提供五个区域:east.west.north.south.center.以下是一些通常用法: north 区域可以用来显示网站的标语. s ...

  10. ADB简易安装方法

    ADB安装方法: 1. 下载ADB程序 下载地址: 链接:https://pan.baidu.com/s/1CfQL51pBz-2Wk_cYfJYXUw  提取码:bjp8 2. 配置环境变量 pat ...