Binary Tree Zigzag Level Order Traversal——LeetCode
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
题目大意:给一个二叉树,将这个二叉树以zigzag形式输出,即一行以从左到右的顺序输出,下一行以从右到左的顺序输出……
解题思路:其实本质还是层次遍历,我们可以用cnt记录层数,奇数层从右到左,偶数层从左到右。用一个List记录这一层的数据,从左到右的顺序插入,从右到左的采用头插法插入即可。
Talk is cheap>>
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
int cnt = 0;
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if ((cnt & 0x1) == 0) {
level.add(node.val);
} else {
level.add(0, node.val);
}
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
res.add(level);
cnt++;
}
return res;
}
Binary Tree Zigzag Level Order Traversal——LeetCode的更多相关文章
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
- Binary Tree Zigzag Level Order Traversal [LeetCode]
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- Binary Tree ZigZag Level Order Traversal leetcode java
题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- 【LeetCode】103. Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
随机推荐
- poj 3565 ants
/* poj 3565 递归分治 还有用KM的做法 这里写的分治 按紫书上的方法 不过那里说的有点冗杂了 可以简化一下 首先为啥可以分治 也就是分成子问题解决 只要有一个集合 黑白的个数相等 就一定能 ...
- Android TV 模拟器启动
模拟器启动错误 使用IntelCPU的模拟器 http://software.intel.com/en-us/android/articles/intel-hardware-accelerated-e ...
- vs里 .sln和.suo 文件
Net解决方案下 .sln文件和.suo文件的解释:When a Web site is created, a solution file (.sln) and a hidden solution u ...
- java_设计模式_工厂模式_Factory Pattern(2016-08-04)
工厂模式主要是为创建对象提供了接口.工厂模式按照<Java与模式>中的提法分为三类: (1)简单工厂(Simple Factory)模式,又称静态工厂方法模式(Static Factory ...
- HDU 3359 Kind of a Blur(高斯消元)
题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B.给定矩阵B,求矩阵A ...
- 运用BeanUtils构建通用的查询 更新方法(个人拙作,不喜勿喷)
------------------------------------更新方法----------------------------------- public void update(Strin ...
- MVC埰坑日记 文件权限
public static void DownLoadFile(string FileFullPath) { if (!string.IsNullOrEmpty(FileFullPath) & ...
- [转载]python os.path模块
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...
- jquery的select元素和option的相关操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JqGrid自定义toolbar
1.设置toolbar参数为[true,"top"],其意思是toolbar显示在Grid顶部,且其id为t_+Grid的id.e.g.: Grid的id为myGrid,toolb ...