Java for LeetCode 103 Binary Tree Zigzag Level Order Traversal
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]
]
解题思路:
直接在上题加一条反转语句即可,JAVA实现如下:
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.size() != 0) {
List<Integer> alist = new ArrayList<Integer>();
for (TreeNode child : queue)
alist.add(child.val);
if(list.size()%2!=0)
Collections.reverse(alist);
list.add(new ArrayList<Integer>(alist));
Queue<TreeNode> queue2 = queue;
queue = new LinkedList<TreeNode>();
for (TreeNode child : queue2) {
if (child.left != null)
queue.add(child.left);
if (child.right != null)
queue.add(child.right);
}
}
return list;
}
Java for LeetCode 103 Binary Tree Zigzag Level Order Traversal的更多相关文章
- leetcode 103 Binary Tree Zigzag Level Order Traversal ----- java
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树来回遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)
原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...
- Leetcode#103 Binary Tree Zigzag Level Order Traversal
原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历
相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- Resolving the Provisioning Profile Invalid Status
Q: What causes the provisioning profile "Invalid" status? How do I resolve it, and how do ...
- Facebook Rebound 弹性动画库 源码分析
Rebound源码分析 让动画不再僵硬:Facebook Rebound Android动画库介绍一文中介绍了rebound这个库. 对于想体验一下rebound的效果,又懒得clone和编译代码的, ...
- 研读:Shielding applications from an untrusted cloud with Haven
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdHJ1c3Ribw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- FenceSyne, flush, wait
我看了下queue, command 的fence这个东西,它是做queque之间 queue和cpu之间同步用的 我理解下来就是这样 有两个condition ALL_GPU_COMMANDS_CO ...
- oracle exchange partition 測试
Exchange partition提供了一种方式,让你在表与表或分区与分区之间迁移数据.注意不是将表转换成分区或非分区的形式,而仅仅仅是迁移表中数 据(互相迁移),因为其号称是採用了更改数据字典的 ...
- dedecms织梦后台password忘记了怎么办?dedecms织梦后台password忘记怎样找回password?
方法一:自己用解密的方式 用phpmyadmin登陆后台数据库,查看 找到password:去除前三位和后一位,然后拷贝到http://www.cmd5.com/在线解密工具里面解密 watermar ...
- poj 1328 Radar Installation 【贪心】【区间选点问题】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54798 Accepted: 12 ...
- 我的IT之路
在写这篇文章的时候内心是无比激动,因为这辈子是注定和IT打交道了. 都说大学时光是美好的,但却只有到了大四才知道时间是短暂的,也许和许多人一样,我的大学主要时光是在游戏中度过,1000多把的寒冰算是同 ...
- ie6中利用jquery居中
1.利用jquery居中代码 <script type="text/javascript"> $hwidth=parseInt($(window).width()); ...
- c# emit 实现类的代理
using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; namespace Em ...