I

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

For example:
Given binary tree{3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".

 /**
* 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> > levelOrder(TreeNode *root) {
vector<vector<int>> res;
if(root==NULL) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
vector<int> v;
for(int i=;i<n;i++){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right);
}
res.push_back(v);
}
return res;
}
};

II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree{3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7]
[9,20],
[3],
]

confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.

先将结果v存入stack中,最后在从stack倒入res形成倒序,未找到其他好的方法

 /**
* 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> > levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if(root==NULL) return res;
stack<vector<int>> s;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int n=q.size();
vector<int> v;
for(int i=;i<n;i++){
TreeNode *cur=q.front();
q.pop();
v.push_back(cur->val);
if(cur->left!=NULL)
q.push(cur->left);
if(cur->right!=NULL)
q.push(cur->right); }
s.push(v);
}
while(!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}
};

binary-tree-level-order-traversal I、II——输出二叉树的数字序列的更多相关文章

  1. 【leetcode】Binary Tree Level Order Traversal I & II

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

  2. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  3. Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>

    问题描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  4. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  5. 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...

  6. Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...

  7. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

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

  8. LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...

  9. 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)

    Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...

  10. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

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

随机推荐

  1. [automator篇][5]Viewer 提示connect to adb fail

    7. UIAutomatorViewer提示: Unable to connect to adb. Check if adb is installed correctly 解决,sdk升级到了25产生 ...

  2. 服务器端查看图片库 eog

    eog 命令 eye of gnome,用来在服务器端查看图片. sudo apt-get install eog eog picname.jpg

  3. Python Mysql学习总结

    任何应用都离不开数据,所以在学习python的时候,当然也要学习一个如何用python操作数据库了.MySQLdb就是python对mysql数据库操作的模块.官方Introduction : MyS ...

  4. C++ Programming with TDD之二:CppUTest单元测试

    在之前一篇C++ Programming with TDD博客中,我带给大家gmock框架的简介(地址戳着里),今天我们继续本系列,带个大家C++中的单元测试框架CppUTest的介绍. CppUTe ...

  5. 【bzoj2729】[HNOI2012]排队 组合数学+高精度

    题目描述 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不同的) 输入 ...

  6. [LOJ#516]「LibreOJ β Round #2」DP 一般看规律

    [LOJ#516]「LibreOJ β Round #2」DP 一般看规律 试题描述 给定一个长度为 \(n\) 的序列 \(a\),一共有 \(m\) 个操作. 每次操作的内容为:给定 \(x,y\ ...

  7. BZOJ 4816 [Sdoi2017]数字表格 ——莫比乌斯反演

    大力反演出奇迹. 然后xjb维护. 毕竟T1 #include <map> #include <ctime> #include <cmath> #include & ...

  8. OpenMP 并行编程

    OpenMP 并行编程 最近开始学习并行编程,目的是为了提高图像处理的运行速度,用的是VS2012自带的OpenMP. 如何让自己的编译器支持OpenMP: 1) 点击 项目属性页 2)点击 配置 3 ...

  9. linux下安装firefox

    首先检查系统有没有安装:rpm -qa|grep firefox 如果有安装,先删掉rpm -e * firefox不同版本下载:http://liulanmi.com/firefox 具体方法如下: ...

  10. cf496D Tennis Game

    Petya and Gena love playing table tennis. A single match is played according to the following rules: ...