LeetCode OJ: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]
]
简单的bfs而已,不过在bfs的过程中应该注意将相应的数组reverse一下,其实都到最终结果之后在隔行reverse也是可以的,下面给出非递归的版本,用递归同样也很好实现:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
struct Node
{
TreeNode * node;
int level;
Node(){}
Node(TreeNode * n, int lv)
: node(n), level(lv){}
};
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ret;
if(!root) return ret;
vector<int> tmp;
int dep = ;
queue<Node>q;
q.push(Node(root, ));
while(!q.empty()){
Node tmpNode = q.front(); //非递归的使用bfs,借助队列特性
if(tmpNode.node->left)
q.push(Node(tmpNode.node->left, tmpNode.level + ));
if(tmpNode.node->right)
q.push(Node(tmpNode.node->right, tmpNode.level + ));
if(dep < tmpNode.level){
if(dep % ){
reverse(tmp.begin(), tmp.end());
}
ret.push_back(tmp);
tmp.clear();
dep = tmpNode.level;
}
tmp.push_back(tmpNode.node->val);
q.pop();
}
if(dep % ){
reverse(tmp.begin(), tmp.end());
}
ret.push_back(tmp);
return ret;
}
};
java版本的代码如下所示,这里用的是dfs而非bfs,在最后重新reverse就可以了:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
dfs(ret, 1, root);
for(int i = 0; i < ret.size(); ++i){
if(i%2 != 0) Collections.reverse(ret.get(i));
}
return ret;
} void dfs(List<List<Integer>>ret, int dep, TreeNode root){
if(root == null)
return;
if(ret.size() < dep){
List<Integer> list = new ArrayList<Integer>();
list.add(root.val);
ret.add(list);
}else
ret.get(dep - 1).add(root.val);
dfs(ret, dep + 1, root.left);
dfs(ret, dep + 1, root.right);
}
}
LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)的更多相关文章
- 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 (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
- 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告
Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...
- 【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 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)
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 ----- 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 _ Medium tag: BFS
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 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 ...
随机推荐
- F110 参数保存和重新运行录屏
**初始界面回车 PERFORM frm_dynpro USING ' 'X'. PERFORM frm_dynpro USING '' 'BDC_CURSOR' 'F110V-LAUFD'. PER ...
- Python基础-文件的基本操作
测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...
- jvm性能监控工具
jvm可能存在的问题: OutOfMemoryError:内存不足 内存泄露 线程死锁 锁竞争(Lock Contention) Java消耗过多的CPU 一.jps(j ...
- 剑指offer 面试68题
面试68题: 题目:求树中两个节点的最低公共祖先 待解决...
- c#中使用NetCDF存储二维数据的读写操作简单应用
[DllImport( [DllImport( [DllImport( ...
- XSS - 禁止浏览器读取Cookie - HttpOnly
1.什么是HttpOnly? 如果您在cookie中设置了HttpOnly属性,那么通过js脚本将无法读取到cookie信息,这样能有效的防止XSS攻击,具体一点的介绍请google进行搜索. C ...
- iOS UIFont 的学习与使用
通常,我们使用字体 都是系统默认的字体. 有时候 从阅读体验,美观度 设计师都会考虑用一些 更高大尚的字体. 系统字体库 给英文 各种style的发挥空间很大,但是 中文则不然. 但是苹果 给使用中文 ...
- C# 调用VS自带程序WebDev.WebServer40.EXE 源代码
通过Process.Start启动,VS自带程序WebDev.WebServer40.EXE 在内网架设网站时,为安装IIS条件下用VS自带的小程序来测试效果非常不错! using System; u ...
- ASP.NET 4.0 ListView等容器控件中获取ClientID值与HTML中自动生成ID字符串不一样问题。
ASP.NET 4.0 中 ClientIDMode的属性 可以设置获取不同ID格式的值. 项目中遇到的问题: 1.ListView1 ItemDataBound事件中,获取ClientID结果与自动 ...
- 混淆Android JAR包的方法
http://blog.csdn.net/vurtne_ye/article/details/35599491 1)导出jar包 如何在eclipse上Android工程中导出jar包?google, ...