这次相对来讲复杂点,题目如下:

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.

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}".

就是按照层数来输出树。思路分为三步,先拿到树的最大层数len,然后再写一个拿指定层的各元素的函数,最后按题目要求写一个返回vector<vector<int>>的函数,里面用一个循环来多次调用之前那个函数,好了,题解如下:

/**
* 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>> temp;
int len = MaxDepth(root); for (int i = len - 1; i >= 0; 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);
}
}
};

因为题目要求是要从底部向上输出,所以在主函数的for循环里用了倒序。

这次的blog用Live Writer发布,测试一下。

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

  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. (二叉树 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 ...

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

  6. leetcode 107 Binary Tree Level Order Traversal II ----- java

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

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

  8. Java [Leetcode 107]Binary Tree Level Order Traversal II

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

  9. 【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. Java 简单TCP文件传输

    服务端 package TCP; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputSt ...

  2. ModuleNotFoundError: No module named '_sqlite3'

    ModuleNotFoundError: No module named '_sqlite3' 解决: 1,首先安装 sqlite-devel yum install sqlite-devel 2,重 ...

  3. shell脚本实现tomcat进程—查、杀、启

    #!/bin/bash#获取XXX项目进程IDXXXpid=`ps -ef | grep XXX | grep -v grep | awk '{print $2}'` echo "XXX项目 ...

  4. sqlplus 方式连接 远程数据库

    方式一:简易连接,不用进行网络配置,其实就是tnsname.ora文件,但只支持oracle10G以上.命令:sqlplus 用户名/密码@ip地址[:端口]/service_name [as sys ...

  5. c# 数据拼接成键值对格式

    public static object FindLayoutTypes() { //地鼓.地裂.墙裂.井水.泉水 var sb = new StringBuilder(); sb.Append(ge ...

  6. zookeeper无故挂掉,启动不起来的问题

    一台测试的服务器重启后,运行zookeeperd的启动脚本显示启动成功: [plain] view plain copy     JMX enabled by default Using config ...

  7. Struts 2 常用技术

    目录  Struts 2 常用技术  1. 常用类和接口  1.1 getter 和 setter 方法  1.2 Action 接口  1.3 ActionSupport 类  1.4 通过 Act ...

  8. 使用.sig签名验证文件

    Linux下载文件的时候,由于网络等原因,下载的文件可能不完整,对于别有心机的人可以更改文件,这就需要我们对文件的完整性进行验证.这里以securityonion-14.04.5.2.iso为例进行验 ...

  9. Go Methods and Interfaces

    [Go Methods and Interfaces] 1.Go does not have classes. However, you can define methods on struct ty ...

  10. python's output redirect

    [python's output redirect] fin = open("xx.txt", 'r'); print >>fin, "hello world ...