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,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its zigzag level order traversal as:

[
[3],
[20,9],
[15,7]
]

==========

按z字输出树节点.

思路:

leetcode上的这种题,都是一个套路,利用BFS遍历方式

要有一个队列queue<TreeNode*> q;保存下一层要访问的节点,

需要一个curr/next计数遍历,curr记录当前层已经访问了几个节点,next记录下一层要访问的节点数量

每访问完一层,需要更新curr和next值,

当访问层数level%2==0时,记录路径反转

=======

code:

/**
* 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 {
public:
///
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> re;
vector<int> path;
if(root==nullptr) return re;
help_zig(root,re,path); for(auto i:re){
for(auto j:i){
cout<<j<<" ";
}cout<<endl;
}cout<<endl;
return re;
}
void help_zig(TreeNode *root,vector<vector<int>> &re,vector<int> &path){
queue<TreeNode*> q;
int curr = ;
int next = ;
q.push(root);
int level = ;
while(!q.empty()){
if(curr>){
TreeNode *tmp = q.front();
path.push_back(tmp->val);
q.pop();
curr--;
if(tmp->left!=nullptr){
q.push(tmp->left);
next++;
}
if(tmp->right!=nullptr){
q.push(tmp->right);
next++;
}
}else{
curr = next;
next = ;
if(level%!=){
reverse(path.begin(),path.end());
}
re.push_back(path);
path.clear();
level++;
}
}///while
if(level%!=) reverse(path.begin(),path.end());
re.push_back(path);
}
};

103. Binary Tree Zigzag Level Order Traversal的更多相关文章

  1. 剑指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 ...

  2. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  3. 【LeetCode】103. Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  4. [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 ...

  5. [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...

  6. 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 ...

  7. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. 103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  9. [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 ...

随机推荐

  1. dedecms调用子栏目内容,缩略图,以及栏目名字

    织梦后台栏目页默认是没有添加缩略图的选项的,所以首先我们要调整后台提交表单使界面出现上传图片的地方,如下图所示:   织梦后台默认是没有栏目缩略图选项的,所以首先你需要从网上下载对应需要修改的文件,从 ...

  2. 使用Hibernate命名查询

    HQL查询支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素来定义命名查 ...

  3. ES6—解构赋值

    1.什么是解构赋值 ES6允许按照预定的模式,从数组.对象中提取值,对变量进行赋值. 我们直接用例子说明.    2. 数组的解构赋值 数组传统的变量赋值:      var arr=[1,2,3]; ...

  4. c 函数及指针学习 4

    1数组和指针声明的差别 声明数组:为数组分配内存,为数组名分配内存(指针常量 4个字节) 指针:为指针分配内存(指针变量 4个字节) 1 2 3 4 5 6 7 8 9 10 #include < ...

  5. Samba服务器安装及配置

    Samba最早诞生在unix操作系统上面,samba是基于SMB(Server Message Block)协议,是一种客户端服务器协议 一.安装samba # yum -y install samb ...

  6. Codeforces Round #135 (Div. 2)

    A. k-String 统计每个字母出现次数即可. B. Special Offer! Super Price 999 Bourles! 枚举末尾有几个9,注意不要爆掉\(long\ long\)的范 ...

  7. UVa 10082 WERTYU

    UVa 10082 题目大意:把手放在键盘上时,稍微不注意就会往右错一位.这样,输入Q就会变成输入W,输入J会变成输入K等等, 输入一个错位后敲出的字符串(所有字母均大写),输出程序员本来想打的句子. ...

  8. hihoCoder#1014 Trie树 (前缀树)

    题目大意:给一本有n个单词的词典,有m次询问,每次询问的是该词典中有多少个单词有共同的某个前缀. 题目分析:在添加单词建立trie的时候,每经过一个节点就意味着该节点和它的各级祖先节点是某个单词的前缀 ...

  9. Improve Scalability With New Thread Pool APIs

    Pooled Threads Improve Scalability With New Thread Pool APIs Robert Saccone Portions of this article ...

  10. Linux-内核缓存区和write行为

    <Unix环境高级编程> 应用缓冲技术能很明显的提高系统效率.内核与外围设备的数据交换,内核与用户空间的数据交换都是比较费时的,使用缓冲区就是为了优化这些费时的操作.其实核心到用户空间的操 ...