LeetCode102 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]
]
分析:
层次遍历二叉树。就是首先訪问二叉树的第一层元素,再訪问第二层,接着訪问第三层。以此类推。
实现的方式是,用一个先进先出的队列作为辅助数据结构。用levelList保存每一层的元素,用resultList保存全部的levelList,然后
(1)把根节点入队列。并把一个哨兵节点入队列。哨兵节点用于标识某一层已经结束
(2)当队列中元素个数大于1时(除哨兵节点外还有其他元素)。进入循环。
訪问该元素,假设该元素为哨兵节点。则说明这一层已经结束了,并将一个哨兵节点入队,用于标识下一层结束的地方,把levelList存入resultList。并建一个新的levelList保存下一层的元素;否则,把该节点的值放进levelList,并把它不为null的孩子节点入队。
(3)把levelList加入resultList。由于最后一个哨兵节点没有办法被訪问到,导致保存最后一层元素的levelList没办法在循环中被加入进resultList。
详细的代码例如以下:
package global;
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
val = x;
}
@Override
public String toString() {
return "TreeNode [val=" + val + ", left=" + left + ", right=" + right
+ "]";
}
}
package leetcode102;
import global.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Created by liyuncong on 10/22/15.
*/
public class LeetCode102 {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
if (root == null) {
return resultList;
}
List<Integer> levelStorage = new LinkedList<Integer>();
LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
queue.offer(null);
while (queue.size() > 1) {
TreeNode top = queue.poll();
if (top == null) {
resultList.add(levelStorage);
queue.offer(null);
levelStorage = new LinkedList<Integer>();
} else {
levelStorage.add(top.val);
if (top.left != null) {
queue.offer(top.left);
}
if (top.right != null) {
queue.offer(top.right);
}
}
}
resultList.add(levelStorage);
return resultList;
}
}
LeetCode102 Binary Tree Level Order Traversal Java的更多相关文章
- lettcode-102:Binary Tree Level Order Traversal (Java)
Binary Tree Level Order Traversal 二叉树的层序遍历 两种方式: 1.用两个queue交替表示每一层的节点 2.用两个node,一个表示当前层的最后一个节点,一个表示下 ...
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- 【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, ...
- 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, ...
- Binary Tree Level Order Traversal java实现
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- leetcode102 Binary Tree Level Order Traversal
""" Given a binary tree, return the level order traversal of its nodes' values. (ie, ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
随机推荐
- 32.AngularJS 表达式
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 表达式写在双大括号内:{{ expression }}. AngularJS 表达式把数据 ...
- Tuple assignment
It is often useful to swap the values of two variables. With conventional assignments, you have to u ...
- Looping and dictionaries
If you use a dictionary in a for statement, it traverses the keys of the dictionary. For example, pr ...
- 11.string容器
#include <iostream> //string的本质也是容器 #include <string> #include <cstdlib> using nam ...
- KafkaProducer的整体逻辑
概述 KafkaProducer是用户向kafka servers发送消息的客户端.官网上对producer的记载如下: Kafka所有的节点都可以应答metadata的请求,这些metadata中包 ...
- AngularJs轻松入门(七)多视图切换
在AngularJs应用中,我们可以將html碎片写在一个单独的文件中,然后在其他页面中將该段碎片加载进来.如果有多个碎片文件,我们还可以在控制器中根据用户的操作动态的加载不同的碎片,从而达到切换视图 ...
- 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码?
go语言的开发工具推荐? go语言开发普通程序及开发web程序的时候,应该以哪种目录结构组织代码? 求推荐go语言开发工具及go语言应该以哪种目录结构组织代码? >> golang这个答案 ...
- Http协议与TCP协议理解(转载的)
TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...
- Linux 中挂载 ISO 文件
在 Linux 中挂载 ISO 文件 用 mount 命令,在终端中输入如下命令即可: sudo mount -o loop filename.iso /cdrom 其中 filename.iso 是 ...
- java 线程传参 方式
第一类:主动向线程传参 public class ThreadTest extends Thread { public ThreadTest() { } /** * 第一种通过构造方法来传递参数 ...