原题地址

基本数据结构操作,二叉树的层次遍历。

代码:

 vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > res;
vector<TreeNode *> layer;
bool l2r = true; layer.push_back(root);
while (!layer.empty()) {
vector<TreeNode *> nextLayer;
vector<int> path;
for (auto node : layer) {
if (!node)
continue;
path.push_back(node->val);
nextLayer.push_back(node->left);
nextLayer.push_back(node->right);
}
if (!l2r) {
for (int i = ; (i << ) < path.size(); i++)
swap(path[i], path[path.size() - i - ]);
}
if (!path.empty())
res.push_back(path);
layer = nextLayer;
l2r = !l2r;
} return res;
}

Leetcode#103 Binary Tree Zigzag Level Order Traversal的更多相关文章

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

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

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

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

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

  6. leetCode 103.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 ...

  7. [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)

    原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...

  8. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历

    相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...

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

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

随机推荐

  1. asp.net mvc JQGrid

    http://mikedormitorio.azurewebsites.net/BlogPost/jqgrid-series-part-1-loading-data-to-a-jqgrid-on-an ...

  2. 一些CSS"bug"

    1.img三像像素问题 解决办法:img{display:block;} or img{vertical-align:middle;} 问题原因:img是行内元素,默认的垂直对齐方式 baseline ...

  3. php输出函数 var_dump, dump,print,print_r 区别

    php输出函数 var_dump, dump,print,print_r 区别 周末有空就来整理下

  4. 一个PHP邮件伪造脚本

    xx.html <html> <head> <title>邮件欺骗</title> <body> <h3>社工必备-邮件欺骗&l ...

  5. hdu 5131 Song Jiang's rank list

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5131 Song Jiang's rank list Description <Shui Hu Z ...

  6. 什么是O/R Mapping(ORM)

    ORM,即Object-Relationl Mapping,它的作用是在关系型数据库和对象之间作一个映射,这样,我们在具体的操作数据库的时候,就不需要再去和复杂的SQL语句打交道,只要像平时操作对象一 ...

  7. linux清除swap

    执行top会显示Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%stMem:   2044500 ...

  8. go语言示例-Timer计时器的用法

    计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...

  9. swift 命令行工具初探

    亲爱的同学们好,今天我们要介绍这么一个东西.相信有过解释型语言(PHP,Ruby,等)使用经验的同学会更加熟悉,就是 Swift 也为我们提供了命令行运行工具,俗称 REPL.好了,我们进入正题,在安 ...

  10. C#语法功能结构

    1.File打开指定文件夹或者文件,"\"为转义字符System.Diagnostics.Process.Start(Application.StartupPath + " ...