Binary Tree Level Order Traversal java实现
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] 实现的关键在于定义两个标记位和队列:
1、标志位last和end。last为记录的是本层次的最后一个最后一个结点,end用于寻找下一层的最后一个结点。
2、队列是用于存储每个结点。
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
List<Integer> lst = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode last =root;
TreeNode end = null;
while(!queue.isEmpty()){
TreeNode t = queue.remove();
if(t!=null){
lst.add(t.val);
if(t.left != null){
queue.add(t.left);
end = t.left;
}
if(t.right != null){
queue.add(t.right);
end = t.right;
}
if(t == last ){
list.add(lst);
last =end;
lst = new ArrayList<>();
}
}
}
return list;
}
}
Binary Tree Level Order Traversal java实现的更多相关文章
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode102 Binary Tree Level Order Traversal Java
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- leetcode 102 Binary Tree Level Order Traversal ----- java
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 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 ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- LeetCode: Binary Tree Level Order Traversal 解题报告
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
随机推荐
- js注册验证【转】
function getFocus() //设置用户名文本框获取焦点 { document.getElementById("txtuname").focus(); } functi ...
- 黑马程序员-C#学习笔记
---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- C#学习笔记 1..NET/.do ...
- 【多媒体封装格式详解】---MKV
http://blog.csdn.net/tx3344/article/details/8162656# http://blog.csdn.net/tx3344/article/details/817 ...
- React事件处理函数的bind复用和name复用
一.bind复用 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&qu ...
- WCF入门(四)---WCF架构
WCF是一个分层架构,为开发各种分布式应用的充分支持.该体系结构在下面将详细说明. 约定 约定层旁边就是应用层,并含有类似于现实世界的约定,指定服务和什么样的信息可以访问它会使操作的信息.约定基本都是 ...
- Spring AOP: Spring之面向方面编程
Spring AOP: Spring之面向方面编程 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解 ...
- C++ 11 vlearning
1.新增算术类型 longlong,最小不比long小,一般为64位. 2.列表初始化 int units_sold = {0};或者 int units_sold{0};非11标准 ...
- Ios tab Bar 使用方法
http://blog.sina.com.cn/s/blog_63578f140100w56m.html UITabBar* tabBar = [[UITabBar alloc] initWithFr ...
- [HDOJ3974]Assign the task(建树胡搞)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 出现在窝bin的线段树专题里…第一时间想的是记录入度找出根节点,然后标记深度转换到线段树中.但是 ...
- CRC校验码
循环冗余校验码(CRC)的基本原理是:在K位信息码后再拼接R位的校验码,整个编码长度为N位,因此,这种编码也叫(N,K)码.对于一个给定的(N,K)码,可以证明存在一个最高次幂为R的多项式G(x)(R ...