以出现的频率来看。树的层序遍历一定是考察的重点,除非工作人员想找题水数量。

zigzag,还是有几道题的,层序的这个非常easy,假设是奇数层。reverse下面就可以。无他。我写的时候预计还不知道这个函数。要么怎么这么拙呢。。

class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > res;
if(root == NULL) return res;
vector<int> tpres;
queue<TreeNode*> que;
TreeNode *pNode;
int level = 0;
que.push(root);
que.push(NULL);
while(!que.empty()){
pNode = que.front();
que.pop();
if(pNode == NULL){
level++;
if(level%2 == 0){
for(int i=0, j=tpres.size()-1;i<j;i++, j--){
int t = tpres[i];
tpres[i] = tpres[j];
tpres[j] = t;
}
}
res.push_back(tpres);
if(que.empty())
break;
else{
tpres.clear();
que.push(NULL);
continue;
}
}
tpres.push_back(pNode->val);
if(pNode->left)
que.push(pNode->left);
if(pNode->right)
que.push(pNode->right);
}
return res;
}
};

leetcode第一刷_Binary Tree Zigzag Level Order Traversal的更多相关文章

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

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

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

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

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

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

  4. LeetCode OJ: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 OJ】Binary Tree Zigzag Level Order Traversal

    Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...

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

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

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

  8. LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

    103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再 ...

  9. 【leetcode】Binary Tree Zigzag Level Order Traversal

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

随机推荐

  1. request与response对象.

    request与response对象. 1. request代表请求对象 response代表的响应对象. 学习它们我们可以操作http请求与响应. 2.request,response体系结构. 在 ...

  2. Python的网络编程[6] -> Modbus 协议 -> Modbus 的基本理论与 Python 实现

    Modbus协议 / Modbus Protocol 目录 Modbus 协议简介 Modbus RTU协议 Modbus TCP协议与 Python 实现 Modbus 功能码 Modbus TCP ...

  3. MyBatis笔记:invalid bound statement (not found)

    maven项目在本地运行的时候没有问题,一旦把war包部署到测试机上就不能运行.查看了一下tomcat日志发现抛出这样的错误:invalid bound statement (not found),后 ...

  4. 加强版dd工具dc3dd

    加强版dd工具dc3dd   dd是Linux最常用的磁盘备份工具,但缺少渗透测试常用的数据校验.hash等重要功能.Kali Linux提供的一款专用工具dc3dd.该工具是dd的加强版.它在dd的 ...

  5. unity3d Billboard

    CameraFacingBillboard   CameraFacingBillboard From Unify Community Wiki   Jump to: navigation, searc ...

  6. 韩国研发AI武器遭抵制,武器自带“头脑”将多可怕

    禁止自主武器的开发,事实上并不妨碍人工智能技术的发展,也不妨碍其被正常用于军事领域,其中的关键就在于,人类是否拥有控制权,能否在关键时刻对其进行关闭. 文 |郑伟彬 转自新京报专栏 4月4日,全球超过 ...

  7. numpy自动生成数组

    1 np.arange(),类似于range,通过指定开始值,终值和步长来创建表示等差数列的一维数组,注意该函数和range一样结果不包含终值. >>> np.arange(10) ...

  8. SWIG 多语言接口变换 【转】

    一.             SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...

  9. RSA加密解密及数字签名Java实现

    http://my.oschina.net/jiangli0502/blog/171263

  10. Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(4)

    Oracle Study之-AIX6.1构建Oracle 10gR2 RAC(4) 一.安装CRS补丁 在安装CRS之前,须要安装补丁p6718715_10203_AIX64-5L,否则在安装时会出现 ...