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. HTTP 学习

    *** *** http://www.w3school.com.cn/xml/xml_http.asp *** *** http://www.cnblogs.com/shenliang123/arch ...

  2. web.config中customErrors与httpErrors的区别

    打开IIS,我们发现会有两个处理错误页的地方,见下图: 进行不同的设置之后,我们发现设定结果会反应在web.config: .NET Error Pages设定被写入system.web/custom ...

  3. 在github上创建新分支

    在github上创建仓库: Create a new repository on the command line touch README.md git init git add README.md ...

  4. 新浪微博客户端(34)-block的细节与本质

    main.m #import <Foundation/Foundation.h> void test4(); int main(int argc, const char * argv[]) ...

  5. VS2010webConfig配置

    1.连接SqlServer数据库 <connectionStrings> <add name="ConnectionStringName" connectionS ...

  6. CF449C Jzzhu and Apples (筛素数 数论?

    Codeforces Round #257 (Div. 1) C Codeforces Round #257 (Div. 1) E CF450E C. Jzzhu and Apples time li ...

  7. matlab 聚类

    目前已知matlab的聚类方法有三种: 一.利用 clusterdata函数对样本数据进行一次聚类,其缺点为可供用户选择的面较窄,不能更改距离的计算方法: 二.层次聚类,该方法较为灵活,需要进行细节了 ...

  8. 梳理javascript原型整体思路

    相信很多对javascript原型初步了解的人都知道prototype,constructor,__proto__这些名词,也在一定程度上可以使用这些对象.属性.甚至知道在构造函数的原型上定义方法供实 ...

  9. SQL 获取查询IO信息

    DBCC DROPCLEANBUFFERS --清空缓存 SET STATISTICS IO ON --开启IO统计 SET STATISTICS TIME ON -- 开启耗时统计 <code ...

  10. 机器码call和jmp地址的计算

    call和jmp都是跳转指令,但是call的同时会把pc地址压入堆栈,并且这两种方式都有远和近跳转.下面的分析不全,因为没有在网上找到足够的资料,个人创造这个情景还是有些困难. 1.例子中的call的 ...