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. 创建守护进程步骤与setsid() -- linux deamon进程

    原创:http://www.cnblogs.com/mickole/p/3188321.html 一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且 ...

  2. 开源面向对象数据库 db4o 之旅,第 1 部分: 初识 db4o

    前言 业界对持久存储领域的追求从未停止过,为了更方便.更容易地用对象表达我们的思维,开源领域和商业领域都涌现了许多新技术, ORM 的出现恰恰说明了这点.最近一年,业界也在反思,到底 ORM 给我们带 ...

  3. 百万级数据查询到datatable中,提示内存溢出

    参考资料: http://group.cnblogs.com/topic/32230.html

  4. 【帖子】怎么彻底删除kafka的topic,然后重建?

    怎么彻底删除kafka的topic,然后重建? 网上都说用kafka-run-class.shkafka.admin.DeleteTopicCommand 命令删除topic,但是并没有成功,用kaf ...

  5. jq实现点击弹出框代码

    废话不多说,先贴代码吧 <script> function showBg() { //定义 showBg 函数 var bh = $("body").height(); ...

  6. 用jquery写循环播放div -2

    前面所说的class html元素标签的写法也要有层次性, 这个层次性其实也就是常说的 css类写法要有一个"命名空间, 名字空间" "namespace" 在 ...

  7. HTML5语义化标签

    在HTML5中最基础也是比较好理解的也就是语义化标签了,,顾名思义语义化也就是可以直接读懂的标签~,这样我们在项目开发过程中不但自己不会因为5花8门的标签命名而伤脑筋,跟同事对接项目也会节约很多时间~ ...

  8. ASP.NET 大文件下载的实现思路及代码

    文件下载是一个网站最基本的功能,ASP.NET网站的文件下载功能实现也很简单,但是如果遇到大文件的下载而不做特殊处理的话,那将会出现不可预料的后果.本文就基于ASP.NET提供大文件下载的实现思路及代 ...

  9. maven 项目配置

    创建java web的maven项目方法有两种,一是先创建maven项目,再选择jdk 和 dynamic web 运行环境 ,二是创建java项目,然后转化为maven项目 1.将普通java项目转 ...

  10. UI第四节——UIImageView详解

    - (void)viewDidLoad { // super调用是必须的 [super viewDidLoad]; UIImage *image = [UIImage imageNamed:@&quo ...