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 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
随机推荐
- GET和POST的区别,就是明信片和信封的区别
- Oracle的学习三:java连接Oracle、事务、内置函数、日期函数、转换函数、系统函数
1.java程序操作Oracle java连接Oracle JDBC_ODBC桥连接 1.加载驱动: Class.forName("sun.jdbc.odbc.JdbcodbcDriver& ...
- TopCoder 649 div1 & div2
最近一场TC,做得是在是烂,不过最后challenge阶段用一个随机数据cha了一个明显错误的代码,最后免于暴跌rating,还涨了一点.TC题目质量还是很高的,非常锻炼思维,拓展做题的视野,老老实实 ...
- hdu2011
http://acm.hdu.edu.cn/showproblem.php?pid=2011 #include<iostream> #include<math.h> #incl ...
- tomcat免重启随意更改java代码 提高开发效率
转载:http://developer.51cto.com/art/201012/241243.htm 做为了一个java开发人员,总是为因为要增加一个类,或是增加删除一个方法,甚至修改一个小处代码而 ...
- 关于NGUI制作图集在低内存设备上的注意事项
正在写一个游戏.由于2D且比较简单.打算用NGUI全权搞定,对,游戏内容也用NGUI. 想的很好,做的很爽.PC上跑起来happy. 天杀的诺基亚出了个手机叫lumia520,可用内存512M.单个程 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- Help And Manual 帮助文件制作工具
Help And Manual 简 介 帮助文件制作工具 支持文件格式 26种 其他功能 制作非常专业的使用手册 一个所见即所得的帮助文件制作工具,是市面上功能最强的 WYSIWYG (所见即所 ...
- 增加oracle数据库最大连接数
这几天碰到系统不能登陆的情况,初步判断可能是数据库连接满了,(后来检查不是这个原因),做了一次增加数据库最大连接数操作.操作步骤如下 操作系统:Red Hat Enterprise Linux Ser ...
- NDK xxxxx could not be resolved解决方法
Type '*****' could not be resolved Method '******' could not be resolved 问题解决 以下为未尝试方法,如果上面方法解 ...