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

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

    Description 考虑一个非递减的整数序列 S1,....Sn+1(Si<=Si+1  1<=i<=n). 序列M1...Mn是定义在序列S的基础上,关系式为 Mi=( Si ...

  2. Bootstrap-Other:HTML编码规范

    ylbtech-Bootstrap-Other:HTML编码规范 1.返回顶部 1. Bootstrap HTML编码规范 语法 用两个空格来代替制表符(tab) -- 这是唯一能保证在所有环境下获得 ...

  3. CFGym 100198G 题解

    一.题目链接 http://codeforces.com/gym/100198/problem/G 二.题意 看样例就能明白,写表达式解析器. 三 .思路 一看这题目,立马就会想到“后缀表达式”,考虑 ...

  4. servlet笔记1

    Myeclipse关于Servlet项目文件的组织方面,如下: WEB-INF:这个目录名称和位置是固定的,放置在该目录下的文件或目录,对外界来说的封闭的,也就是客户端无法用http的任何方式访问到其 ...

  5. leetcode205

    public class Solution { public bool IsIsomorphic(string s, string t) { if (s.Length != t.Length) { r ...

  6. 可视化库-Matplotlib基础设置(第三天)

    1.画一个基本的图 import numpy as np import matplotlib.pyplot as plt # 最基本的一个图,"r--" 线条加颜色, 也可以使用l ...

  7. UNITY 复制对象后局部坐标和世界坐标的变化问题

    void Start () { var pgo = transform.Find ("Button").gameObject; obtn = Instantiate (pgo); ...

  8. 【318】C# 学习笔记

    笔记 01:C# 数据结构 引用类型(Reference types) 引用类型不包含存储在变量中的实际数据,但它们包含对变量的引用. 换句话说,它们指的是一个内存位置.使用多个变量时,引用类型可以指 ...

  9. Vertex color blending & UV tiling

    [Vertex color blending & UV tiling] 1.GemotryData控件用于代码顶点数据,如网格中的Vertex Color(下左图),UV Coord(下右图) ...

  10. SublimeText Videos Notes

    [SublimeText Videos Notes] Getting Started 1.Hello:https://tutsplus.com/course/improve-workflow-in-s ...