Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

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

这里要求层序遍历,而且结果是从下往上,不是从上往下。

思路:1.可以继续从上往下添加到list中,最后反过来放到另一个list中返回

    2:使用list中的add(index,element)方法,该方法是往index位置插入元素,该位置上原来的元素以及后面所有元素往后移,这里只需往0处一直插入就行,就会自动往后移。因为插入会移动后面所有元素,所以此时适合linkedList。

代码1:

class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> tem=new ArrayList<List<Integer>>();
if(root==null) return tem;
Queue<TreeNode> q=new LinkedList<>();
List<Integer> list=new ArrayList<>();
q.add(root);
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode node=q.poll();
list.add(node.val);
if(node.left!=null) q.add(node.left);
if(node.right!=null) q.add(node.right);
}
tem.add(list); list=new ArrayList();
} List<List<Integer>> result=new ArrayList<List<Integer>>();
for(int i=tem.size()-1;i>=0;i--)
result.add(tem.get(i));
return result; }
}

代码2:

  public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> tem=new LinkedList<List<Integer>>();
if(root==null) return tem;
Queue<TreeNode> q=new LinkedList<>();
List<Integer> list=new ArrayList<>();
q.add(root);
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode node=q.poll();
list.add(node.val);
if(node.left!=null) q.add(node.left);
if(node.right!=null) q.add(node.right);
}
tem.add(0,list); list=new ArrayList();
} return tem; }
												

Binary Tree Level Order Traversal II(层序遍历2)的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

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

  2. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

  3. Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>

    问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  4. 102. Binary Tree Level Order Traversal二叉树层序遍历

    网址:https://leetcode.com/problems/binary-tree-level-order-traversal/ 参考:https://www.cnblogs.com/grand ...

  5. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

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

  6. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  7. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  8. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  9. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  10. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

随机推荐

  1. parcel和parcelable

    Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思: 其二为动词,意为打包,扎包.邮寄快递中的包裹也用的是这个词.Android采用这个词来表示封装消息数据.这个是通过IBinder通信 ...

  2. Dynamics CRM 报表导出EXCEL 列合并问题的解决方法

    CRM中的报表导出功能提供了多种格式,excel就是其中之一,这次遇到的问题是导出后打开excel列明合并的问题,具体如下看着相当不美观,物料名称字段占了AB两列,品牌占了CD两列等等. 该问题的源头 ...

  3. Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 为了暂时不影响原来的cat移动方法,我们在CatSprite.m ...

  4. 使用HTML5拍照

    原文连接地址: Camera and Video Control with HTML5 演示地址: HTML5拍照演示 翻译日期: 2013年8月6日 首先,我们看看HTML代码结构,当然,这部分的D ...

  5. iOS编程中的音频知识(二):那么多种格式我应该用哪一个?

    iPhone支持不少格式,比如AAC,HE-AAC,AMR,IMA4等等,你可以在以下网址看到比较全的格式和简要介绍: http://www.raywenderlich.com/69365/audio ...

  6. SQL Server扫盲系列——安全性专题——SQL Server 2012 Security Cookbook

    由于工作需要,最近研究这本书:<Microsoft SQL Server 2012 Security Cookbook>,为了总结及分享给有需要的人,所以把译文公布.预计每周最少3篇.如有 ...

  7. 使用CocoaPods创建Pod

    本来想给App评分,好的开源组件没有Swift版,如是自己写了个简易的.想着既然写了,就写完善点,提供给需要的人使用.这样SwiftyiRate诞生了. 下面主要说下创建pod的步骤: 一.创建git ...

  8. VS2010安装Boost库

    source URL: http://stackoverflow.com/questions/2629421/how-to-use-boost-in-visual-studio-2010 While ...

  9. Android通过编译源代码提供系统服务-android学习之旅(85)

    通过编译android4.1.2的源代码,添加一个FregServer的系统服务,以及一个服务代理FregClient 具体分为三部分,client,common,server,common中规定了c ...

  10. ORM对象关系映射之GreenDAO自定义属性转换器PropertyConverter

    在使用GreenDAO定义实体的属性时候,通常来说定义的实体属性名就是对应的表的字段名.实体中属性的类型(如Long.String等)就是表的字段名类型,但是我们难免会有不一样的需求,比如实体中我定义 ...