LeetCode 二叉树的锯齿形层次遍历
第103题
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回锯齿形层次遍历如下:
[
[3],
[20,9],
[15,7]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal
解题思路
基于二叉树层序遍历改一点代码即可.二叉树层序遍历
- 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的抽象数据类型。双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行。
- 本题和普通的层序遍历区别在于如何正确的选取加入子节点的顺序以及先后.
- 利用双端队列,左进的时候,右出;右进的时候,左出。正好满足一层正序遍历,一层逆序遍历。
代码实现
class Solution103_1 {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
return BFS(root);
}
List<List<Integer>> BFS(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.addLast(root);
List<List<Integer>> result = new LinkedList<>();
boolean reverse = true;
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> subResult = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode node;
if (reverse) {
//头出
node = deque.pollFirst();
//尾进
if (node.left != null) {
deque.addLast(node.left);
}
if (node.right != null) {
deque.addLast(node.right);
}
} else {
//尾出
node = deque.pollLast();
//头进
if (node.right != null) {
deque.addFirst(node.right);
}
if (node.left != null) {
deque.addFirst(node.left);
}
}
subResult.add(node.val);
}
result.add(subResult);
reverse = !reverse;
}
return result;
}
}
完整代码
public class Sub103 {
public static void main(String[] args) {
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
// TreeNode root = new TreeNode(1);
// root.left = new TreeNode(2);
// root.right = new TreeNode(3);
// root.left.left = new TreeNode(4);
// root.right.right = new TreeNode(5);
Solution103_1 solution = new Solution103_1();
List<List<Integer>> list = solution.zigzagLevelOrder(root);
for (List<Integer> subList : list) {
System.out.println(subList.toString());
}
}
}
class Solution103_1 {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
return BFS(root);
}
List<List<Integer>> BFS(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.addLast(root);
List<List<Integer>> result = new LinkedList<>();
boolean reverse = true;
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> subResult = new LinkedList<>();
for (int i = 0; i < size; i++) {
TreeNode node;
if (reverse) {
//头出
node = deque.pollFirst();
//尾进
if (node.left != null) {
deque.addLast(node.left);
}
if (node.right != null) {
deque.addLast(node.right);
}
} else {
//尾出
node = deque.pollLast();
//头进
if (node.right != null) {
deque.addFirst(node.right);
}
if (node.left != null) {
deque.addFirst(node.left);
}
}
subResult.add(node.val);
}
result.add(subResult);
reverse = !reverse;
}
return result;
}
}
资料
LeetCode 二叉树的锯齿形层次遍历的更多相关文章
- LeetCode:二叉树的锯齿形层次遍历【103】
LeetCode:二叉树的锯齿形层次遍历[103] 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- Java实现 LeetCode 103 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null ...
- leetcode 102. 二叉树的层次遍历 及 103. 二叉树的锯齿形层次遍历
102. 二叉树的层次遍历 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / ...
- LeetCode103. 二叉树的锯齿形层次遍历
103. 二叉树的锯齿形层次遍历 描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 示例 例如,给定二叉树: [3,9,2 ...
- 【二叉树-BFS系列1】二叉树的右视图、二叉树的锯齿形层次遍历
题目 199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, ...
- lintcode二叉树的锯齿形层次遍历 (双端队列)
题目链接: http://www.lintcode.com/zh-cn/problem/binary-tree-zigzag-level-order-traversal/ 二叉树的锯齿形层次遍历 给出 ...
- lintcode: 二叉树的锯齿形层次遍历
题目 二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / \ ...
- LintCode-71.二叉树的锯齿形层次遍历
二叉树的锯齿形层次遍历 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 返回其锯齿形的层次 ...
随机推荐
- element中 input赋值后无法再次输入值
项目中有个需求,在表格里点击某条数据弹出窗口进行修改值,当时弹出的是input上进行修改,所以当我点击数据的时候,先进行回显原先的数据,再进行修改. 点击某条数据,弹出窗口,进行后台请求,将后台返回的 ...
- React - 组件:类组件
目录: 1. 类组件有自己的状态 2. 继承React.Component-会有生命周期和this 3. 内部需要一个render函数(类组件会默认调用render方法,但不会默认添加,需要手动填写r ...
- UILabel(label控件)的详细使用及特殊效果
转自:http://blog.sina.com.cn/s/blog_af73e7a70101ahlm.html UILabel *label = [[UILabelalloc] initWithFra ...
- 关于maven-assembly-plugin插件打包,有部分无法打包的情况解决方法
今天在使用maven-assembly-plugin 对生产者进行打包,然后在linux中发布时.将包打包之后,发现mybtis的xml无法识别,然后查看原因说是没有这个包,我当时就纳闷了,都是基操( ...
- FIve in a row
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put eit ...
- 洛谷 SPOJ 题解 SP1 【TEST - Life, the Universe, and Everything】
给出一种主函数递归的方法(其实主函数 main() 也是可以递归的) #include <stdio.h> int main() { int a; scanf("%d" ...
- ARTS-S CentOS 7 minimal 版本安装后网络配置
用root登录服务器,执行 nmcli d 可以看到ethernet disconnected,网卡是处于禁用状态.执行 nmtui 选Edit a connection-Edit,选中Automat ...
- windows程序设计03_读取utf8文件
这里用到的读取utf8文件的思路特别朴素.先把utf8文件按char读取到内存里.因为utf8是变长的,为了处理方便,在内存里把char转化成wchar_t,这样一个字符就是一个wchar_t.把ut ...
- 基于 Blazui 的 Blazor 后台管理模板 BlazAdmin 正式尝鲜
简介 BlazAdmin 是一个基于Blazui的后台管理模板,无JS,无TS,非 Silverlight,非 WebForm,一个标签即可使用. 我将在下一篇文章讨论 Blazor 服务器端渲染与客 ...
- display:none和visibility:hidden两者的区别
display与元素的隐藏 如果给一个元素设置了display: none,那么该元素以及它的所有后代元素都会隐藏,它是前端开发人员使用频率最高的一种隐藏方式.隐藏后的元素无法点击,无法使用屏幕阅读器 ...