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,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]

第二遍方法:

这道题在groupon面经里面有,有一个follow up 是能不能右对齐输出。那就在29行记录每一行的最大size,然后在输出的时候根据最大size补齐空格

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> item = new ArrayList<Integer>();
int size = queue.size();
for (int i=0; i<size; i++) {
TreeNode cur = queue.poll();
item.add(cur.val);
if (cur.left != null) {
queue.add(cur.left);
}
if (cur.right != null) {
queue.add(cur.right);
}
}
res.add(0, new ArrayList<Integer>(item));
}
return res;
}
}

在Binary Tree Level Order Transversal的基础上难度:20,只需要对最后结果做一个倒序就好。格式是Collections.reverse(List<?> list)

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>> ();
if (root == null) return lists;
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
int ParentNumInQ = 1;
int ChildNumInQ = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
list.add(cur.val);
ParentNumInQ--;
if (cur.left != null) {
queue.add(cur.left);
ChildNumInQ++;
}
if (cur.right != null) {
queue.add(cur.right);
ChildNumInQ++;
}
if (ParentNumInQ == 0) {
ParentNumInQ = ChildNumInQ;
ChildNumInQ = 0;
lists.add(list);
list = new ArrayList<Integer>();
}
}
Collections.reverse(lists);
return lists;
}
}

注意38行的写法,Collections.reverse()跟Arrays.sort()函数一样,都是void返回型,然后改变作用在argument上

还有if (root == null) return null;

ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();这样会出错:

Input:{}Output:nullExpected:[]

DFS 做法:

 public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
levelMaker(wrapList, root, 0);
return wrapList;
} public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
if(root == null) return;
if(level >= list.size()) {
list.add(0, new LinkedList<Integer>());
}
levelMaker(list, root.left, level+1);
levelMaker(list, root.right, level+1);
list.get(list.size()-level-1).add(root.val);
}
}

Leetcode: Binary Tree Level Order Transversal II的更多相关文章

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

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

  2. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  3. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

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

  4. LeetCode——Binary Tree Level Order Traversal II

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

  5. LeetCode - Binary Tree Level Order Traversal II

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

  6. Leetcode: Binary Tree Level Order Transversal

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

  7. LeetCode "Binary Tree Level Order Traversal II" using DFS

    BFS solution is intuitive - here I will show a DFS based solution: /** * Definition for a binary tre ...

  8. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  9. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

随机推荐

  1. 【BZOJ1478】Sgu282 Isomorphism Pólya定理神题

    [BZOJ1478]Sgu282 Isomorphism 题意:用$m$种颜色去染一张$n$个点的完全图,如果一个图可以通过节点重新标号变成另外一个图,则称这两个图是相同的.问不同的染色方案数.答案对 ...

  2. 在python pydev中使用todo标注任务

    在做自动化测试时,有部分代码因需求未定或界面需要更改,代码不做修改或更新,这里就需要用到TODO功能. 在PyCharm中TODO功能很详细,但在pydev中怎么用呢.看了文档后,截图如下: 1.设置 ...

  3. Java秒杀简单设计一:搭建springboot环境

    项目参考:慕课网  https://www.imooc.com/learn/587 Java秒杀 开发环境 JDK1.8.Maven.Mysql.Eclipse.SpringBoot2.0.5.myb ...

  4. Android开发中Chronometer的用法

    Chronometer集成自TextView,里面有个Handler负责定时更新ui. 其计时原理很简单:通过setBase(long t)方法设置好baseTime之后,当start()时,每隔一秒 ...

  5. 9.20Ajax知识sweetalet

    2018-9-20 14:19:55 2018-9-20 21:33:05 周末可以帮我图书商城再次优化一下!! 加入 Ajax请求,,再加上 sweetAlert  甜蜜对话框插件! 要是再加上模态 ...

  6. 7.24python协程(2)和IO模型

    2018-7-24 08:50:29 异步IO模型 epoll  机制  linux 给每个监听对象绑定回调函数,当要读的对象来了时候,回调函数直接被执行,然后通知用户,效率非常高! python无法 ...

  7. 微信小程序中target与currentTarget

    target在事件流的目标阶段:currentTarget在事件流的捕获,目标及冒泡阶段.但事件流处于目标阶段,target与currentTarget指向一样, 而当处于捕获和冒泡阶段的时候,tar ...

  8. Asp.net中的web.config配置

    目录 Asp.net中的web.config配置... 1 一. 配置文件保存位置... 2 二. 配置文件加载顺序... 2 三. 配置文件节点介绍... 3 1. . 3 2. . 5 3. . ...

  9. kdevelop使用笔记

    https://www.cnblogs.com/-Mr-y/p/7707176.html#_label7

  10. Oracle体系结构之数据文件管理

    数据文件分2个方向管理: 物理结构和逻辑结构. 数据库的存储层次结构图: ............. 逻辑结构:                                  物理结构: .... ...