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]
]
class Solution {
public:
vector > res;
public:
void trval(TreeNode *root,int level)
{
if(root==NULL)
return;
if(level==res.size()) //这句是关键啊
{
vector v;
res.push_back(v);
}
res[level].push_back(root->val);
trval(root->left,level+);
trval(root->right,level+);
}
vector > levelOrder(TreeNode *root) { trval(root,);
return res; }
};

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]
]
/**
* 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> > res;
void order(TreeNode *root,int level)
{
if(root==NULL)
return ; if(res.size()==level)
{
vector<int> tmp;
res.push_back(tmp);
} res[level].push_back(root->val);
order(root->left,level+);
order(root->right,level+);
}
vector<vector<int> > levelOrderBottom(TreeNode *root) {
order(root,);
return vector<vector<int> > (res.rbegin(),res.rend()); //直接一句话解决了 }
};

Binary Tree Level Order Traversal&&II的更多相关文章

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

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

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

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

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

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

  6. LeetCode_107. Binary Tree Level Order Traversal II

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

  7. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

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

  9. 【Binary Tree Level Order Traversal II 】cpp

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

  10. Binary Tree Level Order Traversal II 解题思路

    思路: 与Binary Tree Level Order Traversal I 几乎一样.只是最后将结果存放在栈里,然后在栈里再传给向量即可. 再次总结思路: 两个queue,先把第一个放进q1,循 ...

随机推荐

  1. 使用openssl实现RSA非对称加密

    生成公钥私钥 使用命令生成私钥 openssl genrsa - 参数:genrsa 生成密钥   -out 输出到文件  rsa_private_key.pem 文件名  1024 长度 从私钥中提 ...

  2. 剑桥offer系列(1~10)

    1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路:从左下开始, ...

  3. Install JDK In Ubuntu

    安装Linux软件包管理器rpm apt install rpm 查看已安装的软件,如JDK rpm -qa|grep jdk #查询所有 找jdk 卸载已安装的软件 rpm -e nodeps 包名 ...

  4. Codeforces Round #332 (Div. 2)B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  5. Trades FZU - 2281 (大数+贪心)

    This is a very easy problem. ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n ...

  6. rm删除破折号 - 开头的文件

    解决这个问题的一个方法就是在要删除的文件的前边加上"./" # rm ./-slow_query_130103.txt.gz To remove a file whose name ...

  7. JQuery学习一

    第一个JQuery程序 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  8. Spark Streaming 的一些问题

    Spark Streaming 的一些问题,做选型前关注这些问题可以有效的降低使用风险. checkpoint checkpoint 是个很好的恢复机制.但是方案比较粗暴,直接通过序列化的机制写入到文 ...

  9. Centos下iptables常用命令

    安装iptablesyum install iptables-services 重启防火墙使配置文件生效systemctl restart iptables.service 设置iptables防火墙 ...

  10. 2015/9/15 Python基础(12):模块和包

    模块是用来组织 Python 代码的方法,而包则是用来组织模块的. 当代码量很大时,我们一般会把代码分成几个有组织的代码段,然后每个代码段之间有一定的联系.代码单之间是共享的,所以Python允许调入 ...