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]
]
confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
bool reverse=false;
while(!q.empty()){
vector<int> v;
int size=q.size();
for(int i=;i<size;++i){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL) q.push(cur->left);
if(cur->right!=NULL) q.push(cur->right);
}
if(reverse){ vector<int> tmp;
for(int i=v.size()-;i>=;--i){
tmp.push_back(v[i]);
}
res.push_back(tmp);
}else{
res.push_back(v);
}
reverse=!reverse;
}
return res;
}
};
binary-tree-zigzag-level-order-traversal——二叉树分层输出的更多相关文章
- [LeetCode] 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] 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二叉树来回遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7], 3 ...
- Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历
相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...
- 剑指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 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...
- Binary Tree Zigzag Level Order Traversal (LeetCode) 层序遍历二叉树
题目描述: Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474 My Submissions Given a binary tree, ...
随机推荐
- Ansible实战之Nginx代理Tomcat主机架构
author:JevonWei 版权声明:原创作品 实验架构:一台nginx主机为后端两台tomcat主机的代理,并使用Ansible主机配置 实验环境 Nginx 172.16.252.82 Tom ...
- 【bzoj1316】树上的询问 树的点分治+STL-set
题目描述 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. 输入 第一行两个整数n, p分别表示点的个数和询问的个数. 接下来n-1行每行 ...
- mongodb学习(3)--- NodeJs使用mongoose操作mongodb
转载: https://cnodejs.org/topic/50c145ed637ffa4155c7eaee 首先对于以下错误说明(有写 db.close): Error: db object alr ...
- JavaScript 的新特性:类的 #private 字段
这是什么,如何使用,为什么需要? 一边听“Noise Pollution” —— Portugal. The Man,一边阅读本文简直就是享受 JavaScript 标准的第二阶段(Stage 2)加 ...
- Linux 命令行下使用多行输入
比较简单,建议实操,直接上图: 一行结束,直接敲回车换行.上一个例子,输入eof,终止多行输入:下一个例子,输入done,终止多行 ~~ 如果是参数太多,一行输入不完,可以通过 "空格\en ...
- ai相关
学习资源 1.1 1.2 2.1 2.2 2.3 前置 octave sklearn python3 git 学习相关 link 定义 Field of study that gives comput ...
- 背景.jpg
- 利用input-radio和input-checkbox的表单特性可以节省很多js代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- c语言中的rewind函数,Win CE 不支持,可用fseek函数替换
FILE *read = fopen(cXmlFile,"rb"); if (read) { fseek(read, 0L, SEEK_END); int len = ftell( ...