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]
]
 
 
利用队列,进行层次遍历
根据层的奇偶性,判断是否需要翻转
 
 
 /**
* 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> > result;
queue<TreeNode*> q; if(root==NULL) return result;
q.push(root); int numNode=; bool isOdd=true; while(!q.empty())
{
vector<int> cur;
int nextLevelCount=;
for(int i=;i<numNode;i++)
{
TreeNode *node=q.front();
cur.push_back(node->val);
q.pop();
if(node->left!=NULL)
{
q.push(node->left);
nextLevelCount++;
} if(node->right!=NULL)
{
q.push(node->right);
nextLevelCount++;
}
} if(isOdd)
{
result.push_back(cur);
}
else
{
reverse(cur.begin(),cur.end());
result.push_back(cur);
} isOdd=!isOdd; numNode=nextLevelCount;
} return result; }
};

【leetcode】Binary Tree Zigzag Level Order Traversal的更多相关文章

  1. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

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

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

  3. 【Leetcode】【Medium】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. 【树】Binary Tree Zigzag Level Order Traversal

    题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from lef ...

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

  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二叉树来回遍历

    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 _ Medium tag: BFS

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

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

随机推荐

  1. Bitcask 存储模型

    Bitcask 存储模型 Bitcask 是一个日志型.基于hash表结构的key-value存储模型,以Bitcask为存储模型的K-V系统有 Riak和 beansdb新版本. 日志型数据存储 何 ...

  2. Orchard源码分析(4.4):Orchard.Caching.CacheModule类

    概述 CacheModule也是一个Autofac模块.   一.CacheModule类 CacheModule将DefaultCacheManager注册为ICacheManager:       ...

  3. BAT command

    http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.html BAT常用命令 1.@ 它的作用是隐藏它后面这一行的命令本身(只能 ...

  4. php生成唯一随机码

    最终使用: echo md5(time() . mt_rand(1,1000000)) //A:利用时间戳的方法 md5("admin"); // B:32位MD5加密 subst ...

  5. yaf自动加载文件

    models下面的文件 例如:Article.php 类名必须是:ArticleModel 调用时:$article_models = new ArticleModel(); library下面的文件 ...

  6. 配置lamp中的apache

    root@komiles-VirtualBox:/etc/apache2/sites-available# service apache2 restart * Restarting web serve ...

  7. SASS的一些使用体会(安装-配置-开启firefox的调试)

    对CSS预处理这个东西的看法,基本就是2种 第一种:不就是css吗,我会写就好了搞得那么复杂干嘛 第二种:感觉这个东西非常有必要,它规范了代码,使开发变得更轻松 好吧以前我是第一种,并且觉得又要配置环 ...

  8. 用GL画出人物的移动路径

    注意:用Debug画的线会存在穿透问题 没啥好解释的,直接看代码: using UnityEngine; using System.Collections; using System.Collecti ...

  9. Mac OS 中的 Python(和 NumPy)开发环境设置

    http://www.python()tab.com/html/2013/pythonjichu_1010/582.html ()需要删除

  10. lua操作json,mysql,redis等

    ==========================example for lua json======================= local cjson = require("cj ...