lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal
二叉树的层序遍历
两种方式:
1、用两个queue交替表示每一层的节点
2、用两个node,一个表示当前层的最后一个节点,一个表示下一层的最后一个节点
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.Queue;
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> list = new ArrayList<List<Integer>>();
if(root == null)
return list;
Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
Queue<TreeNode> queue2 = new ArrayDeque<TreeNode>();
queue.offer(root); List<Integer> listLevel = new ArrayList<Integer>(); TreeNode curNode = null;
while( !queue.isEmpty() || (!queue2.isEmpty())) {
while(!queue.isEmpty()){
curNode = queue.poll();
listLevel.add(curNode.val);
if(curNode.left != null) queue2.offer(curNode.left);
if(curNode.right != null) queue2.offer(curNode.right); if(queue.isEmpty()){
list.add(listLevel);
listLevel = new ArrayList<Integer>();
}
} while(!queue2.isEmpty()){
curNode = queue2.poll();
listLevel.add(curNode.val);
if(curNode.left != null) queue.offer(curNode.left);
if(curNode.right != null) queue.offer(curNode.right); if(queue2.isEmpty()){
list.add(listLevel);
listLevel = new ArrayList<Integer>();
}
} } /** 两个node,
* lastNode: 当前层的最后一个节点
* lastInqueue:表示某层最后进入队列的节点
TreeNode curNode = root;
TreeNode lastNode = root;
TreeNode lastInQueue = root;
while(!queue.isEmpty()){
curNode = queue.poll();
listLevel.add(curNode.val); if(curNode.left != null){
queue.offer(curNode.left);
lastInQueue = curNode.left;
}
if(curNode.right != null){
queue.offer(curNode.right);
lastInQueue = curNode.right; } if(curNode == lastNode){
list.add(listLevel);
// get last node
lastNode = lastInQueue;
listLevel = new ArrayList<Integer>(); }
}
*/ return list;
}
}
lettcode-102:Binary Tree Level Order Traversal (Java)的更多相关文章
- 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, ...
- [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, ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- [刷题] 102 Binary Tree Level Order Traversal
要求 对二叉树进行层序遍历 实现 返回结果为双重向量,对应树的每层元素 队列的每个元素是一个pair对,存树节点和其所在的层信息 1 Definition for a binary tree node ...
- Java for 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, ...
- Java [Leetcode 102]Binary Tree Level Order Traversal
题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...
- 102. Binary Tree Level Order Traversal
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- 【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, ...
随机推荐
- 随便写写,当作了解--Css
Css,Cascading Style Sheets,层叠样式表.用于控制HTML页面样式.他的基本格式由两部分组成: 选择器 声明块 一.使用 css的注释用 /* 注释内容 */ 1.导入外部样式 ...
- MATLAB中如何使用遗传算法
matlab有遗传算法工具箱. 核心函数: (1)function [pop]=initializega(num,bounds,eevalFN,eevalOps,options)--初始种群的生成 ...
- Spring中bean的scope
Spring容器中的bean具备不同的scope,最开始只有singleton和prototype,但是在2.0之后,又引入了三种类型:request.session和global session,不 ...
- 使用开源库PhotoView
一.下载地址:https://github.com/chrisbanes/PhotoView 点击DownLoad ZIP,进行下载. 二.解压,然后复制PhotoView-master\librar ...
- 【转】web测试方法总结
一.输入框 1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时 ...
- 【转】adb uninstall卸载apk文件说明
昨天在使用adb卸载程序,结果死活卸载不了.我输入的命令和系统提示如下: [plain] view plaincopy arthur@arthur-laptop:~$ adb uninstall ...
- ASP大数据量使用GetRows()提升速度
抽取10万条数据,Access数据库,GetRows() 现有10W条数据,Access数据库保存 通过正常提取: <% Set conn= Server.CreateObject(" ...
- C++的转换构造函数、拷贝构造函数、赋值运算符重载
1 转换构造函数 C++的转换构造函数是只有一个参数的构造函数.当程序试图将一个其他类型的对象或基本类型值赋给该类的一个待初始化对象时(如Person p="Dean";) ...
- C#学习笔记8:HTML和CSS基础学习笔记
<!-- 1.<P>...</P>段落标签 2.<br/>折行标签. 3.<img src="" height="*px& ...
- C#学习笔记4:关键词大小写、复合格式化等
1.取消选择的单选按钮值 RadioButton1.Checked=RadioButton2.Checked = false;//RadioButton1.Text = RadioButto ...