给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

例如:

给定二叉树 [3,9,20,null,null,15,7],

3 / \ 9 20 / \ 15 7

返回锯齿形层次遍历如下:

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

 class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode* root)
{
vector<vector<int> > res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
int cnt = 0;
while(!q.empty())
{
int size = q.size();
vector<int> temp;
for(int i = 0; i < size; i++)
{
TreeNode *node = q.front();
q.pop();
temp.push_back(node ->val);
if(node ->left)
q.push(node ->left);
if(node ->right)
q.push(node ->right);
}
if((cnt & 1) == 1)
{
reverse(temp.begin(), temp.end());
}
res.push_back(temp);
cnt++;
}
return res;
}
};

Leetcode103. Binary Tree Zigzag Level Order Traversal二叉树的锯齿形层次遍历的更多相关文章

  1. 103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历

    给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行).例如:给定二叉树 [3,9,20,null,null,15,7],    3   ...

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

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

  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. (二叉树 BFS) leetcode103. Binary Tree Zigzag Level Order Traversal

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

  6. 32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

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

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

  9. LeetCode103 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. 逆元 组合A(n,m) C(n,m)递推 隔板法

    求逆元 https://blog.csdn.net/baidu_35643793/article/details/75268911 int inv[N]; void init(){ inv[] = ; ...

  2. python+selenium中webdriver相关资源

    Chrome chrome的webdriver :  http://chromedriver.storage.googleapis.com/index.html chrome的webdriver需要对 ...

  3. Python中死锁的形成示例及死锁情况的防止

    死锁示例搞多线程的经常会遇到死锁的问题,学习操作系统的时候会讲到死锁相关的东西,我们用Python直观的演示一下.死锁的一个原因是互斥锁.假设银行系统中,用户a试图转账100块给用户b,与此同时用户b ...

  4. thinkphp Mongo模型

    Mongo模型是专门为Mongo数据库驱动而支持的Model扩展,如果需要操作Mongo数据库的话,自定义的模型类必须继承Think\Model\MongoModel. Mongo模型为操作Mongo ...

  5. android 头像选择以及裁剪

    一.布局申明 <ImageView android:id="@+id/head_image" android:layout_width="80dp" an ...

  6. 表单单选按钮input[type="radio"]

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  7. GCC 参数详解

    转载出处:http://blog.csdn.net/yff1030/article/details/8592077 原文:http://www.cppblog.com/SEMAN/archive/20 ...

  8. 渗透测试入门DVWA 环境搭建

    DVWA是一款渗透测试的演练系统,在圈子里是很出名的.如果你需要入门,并且找不到合适的靶机,那我就推荐你用DVWA. 我们通常将演练系统称为靶机,下面请跟着我一起搭建DVWA测试环境.如果你有一定的基 ...

  9. Django 异步任务、定时任务Celery

    将任务分配给其他的进程去运行,django的主进程只负责发起任务,而执行任务的不在使用django的主进程.Python有一个很棒的异步任务框架,叫做celery. Django为了让开发者开发更加方 ...

  10. redis String 命令

    今天在虚拟机的Ubuntu上装了一个redis,学习redis的一些基本东西,在数据类型的时候,看到redis的,String,hash,set list zset,对String的setbit命令一 ...