【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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]
]

题目大意

  给定一棵二叉树自底向下进行层序遍历。

解题思路

  对树进行层序遍历,每层的结果放在结果链表的头部。

代码实现

public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

算法实现类

import java.util.*;

public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) { List<List<Integer>> list = new LinkedList<>();
if (root == null) {
return list;
} Deque<TreeNode> cur = new LinkedList<>();
Deque<TreeNode> nxt = new LinkedList<>();
Deque<TreeNode> exc = new LinkedList<>(); TreeNode tmp; cur.add(root);
while (!cur.isEmpty()) {
List<Integer> layout = new ArrayList<>(); while (!cur.isEmpty()) {
tmp = cur.remove(); if (tmp.left != null) {
nxt.add(tmp.left);
} if (tmp.right != null) {
nxt.add(tmp.right);
} layout.add(tmp.val);
} exc = cur;
cur = nxt;
nxt = exc; list.add(0, layout);
} return list;
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置。释放后在新的窗体中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47392965

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】的更多相关文章

  1. [LeetCode]题解(python):107 Binary Tree Level Order Traversal II

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...

  2. Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)

    来源:https://leetcode.com/problems/binary-tree-level-order-traversal Given a binary tree, return the l ...

  3. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

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

  5. Java for LeetCode 107 Binary Tree Level Order Traversal II

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

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. [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 ...

  8. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

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

  9. 【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 ...

  10. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

随机推荐

  1. C#打印日志的小技巧(转)

    https://www.cnblogs.com/jqg-aliang/p/5234206.html 打印日志的函数 开发中输出日志必不可少,在C#中输出多个不同类型参数的时候,需要连接符累加输出,很是 ...

  2. 阅读笔记——Servlet

    什么是Servlet Servlet是用java编写的运行在web服务器中的程序,因此它可以调用服务器端的类,它也可以被调用,它本身就是一个类. Servlet的工作原理 servlet由web服务器 ...

  3. [Javascript AST] 1. Continue: Write a simple Babel plugin

    We want to write a Babel Plugin, which move 'const versionRegex = /(/d+)\.(/d+)\.(/d+)/gi' out of fu ...

  4. C# 依据KeyEventArgs与组合键字符串相互转换

    /// 快捷键相关的类 /// </summary> public static class HotKeyInfo { /// <summary> /// 依据KeyEvent ...

  5. ontouch、dispatchtouchevent、interceptouchevent-相关事件

    这几天一直在研究onTouch的相关方法,今天我们就来看看onTouchEvent.dispatchTouchEvent.onIntercepTouchEvent这三个方法在控件之间的传递顺序 pub ...

  6. 1.2 Use Cases中 Website Activity Tracking官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Website Activity Tracking 网站活动追踪 The origi ...

  7. StringBuilder类的使用总结

    String 对象是不可改变的.每次使用 System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间.在需要对字符串执行重复修改的情况下,与创建新 ...

  8. HSV颜色模型

    HSV(Hue, Saturation, Value)是根据颜色的直观特性由A. R. Smith在1978年创建的一种颜色空间, 也称六角锥体模型(Hexcone Model). 注意的是OpenC ...

  9. sql语句的编程手册 SQL PLUS

    一.SQL PLUS 引言 SQL命令 以下17个是作为语句开头的关键字: alter drop revoke audit grant rollback* commit* insert select ...

  10. Java中关于static语句块的理解

    Java中关于static语句块的理解 一.static块会在类被加载的时候执行且仅会被执行一次,一般用来初始化静态变量和调用静态方法. 实例一 public class A{ String name ...