跟第七题一样,把最后的输出顺序换一下就行。。。

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.

题解如下:

/**
* 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>> temp;
int len = MaxDepth(root); for (int i = 0; i < len; i++)
{
vector<int> level;
getElement(level, 0, i, root); temp.push_back(level);
level.clear();
} return temp; } int MaxDepth(TreeNode *temp)
{
if (temp == NULL)
return 0;
else
{
int aspros = MaxDepth(temp->left);
int defteros = MaxDepth(temp->right);
return 1 + (aspros>defteros ? aspros : defteros);
}
} void getElement(vector<int> &level, int count, int len, TreeNode *root)
{
if (root != NULL)
{
if (count == len)
{
level.push_back(root->val);
}
getElement(level, count + 1, len, root->left);
getElement(level, count + 1, len, root->right);
}
}
};

[leetcode] 9. Binary Tree Level Order Traversal的更多相关文章

  1. Java for LeetCode 107 Binary Tree Level Order Traversal II

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

  2. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  3. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

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

  4. LeetCode(32)-Binary Tree Level Order Traversal

    题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...

  5. [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

  6. 【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, ...

  7. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  8. (二叉树 BFS) leetcode 107. 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. LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)

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

  10. [Leetcode][JAVA] Binary Tree Level Order Traversal

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

随机推荐

  1. jsp---猜数字游戏,深有感触

    猜数字游戏注意两点.1.随机数和猜的数字不能放在同一个页面,不然随机数不停出现,猜的数字不可能相等的. 2.数据类型的相互转换.包装类Integer和int的用法,前者是类,后者是基本数据类型 cai ...

  2. python环境搭建,开发环境

    一.从链接http://www.python.org/downloads/ 下载安装包,注意32位和64位安装包区别. https://www.python.org/downloads/windows ...

  3. HDU 2199 Can you solve this equation?(二分精度)

    HDU 2199 Can you solve this equation?     Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == ...

  4. [C#][控件]WebBrowser 使用范例

    if (webInfo.Document != null) webInfo.Document.OpenNew(true); else webInfo.Navigate("about:blan ...

  5. mysql-13处理重复数据

    1.防止表中出现重复数据 在mysql数据表中设置指定的字段为主键或唯一索引来保证数据的唯一行. -- 方法1:指定主键 create `table person_tbl`( `first_name` ...

  6. zabbix触发器函数 count

    转摘至梅总文章 一直没用过这个函数,今天研究了下,确实很有用(用过的忽略): 之前很多功能都是用max,min,avg曲线实现的,其实用count最合理(如典典刚用的高防持续N次ping超时).   ...

  7. UI“三重天”之selenium--常用API和问题处理(三)

    Selenium常用API: 前面两篇示例代码中用到了一些selenium的API方法,例如定位元素的八种方法.访问url.等待.操作浏览器.获取title.点击.清理等等. 有关于selenium的 ...

  8. vim配置vimrc

    新建文件,自动加入文件头 修改文件,保存时,自动更新修改时间字段 自动匹配括号,引号等 vimrc文件如下 "======================================== ...

  9. C# DataTable添加行和列

    方法一: DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Colu ...

  10. Python实践练习:选择性拷贝

    题目 项目要求:编写一个程序,遍历一个目录树,查找特定扩展名的文件(诸如.pdf 或.jpg),不论这些文件的位置在哪里, 将它们拷贝到一个新的文件夹中. 代码 import os import sh ...