一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源: https://leetcode.com/problems/binary-tree-level-order-traversal-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,null,null,15,7],

3

/ \

9 20

/ \

15 7

return its bottom-up level order traversal as:

[

[15,7],

[9,20],

[3]

]

(二)解题

本题大意:按层序从下往上输出二叉树,注意与【一天一道LeetCode】#102. Binary Tree Level Order Traversal

解题思路:在上一题的基础上,我偷懒的用了一个resverse函数就将结果反过来输出了。

代码如下:

/**
 * Definition for a binary tree node.
 * 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>> ret;
        if(root==NULL) return ret;
        queue<TreeNode*> que;//用queue来存储每一层的节点
        que.push(root);//初始化
        while(!que.empty())
        {
            vector<int> tempVec;
            queue<TreeNode*> tempque;
            while(!que.empty())//依次取出节点
            {
                TreeNode* tempNode = que.front();//从左往右
                que.pop();
                tempVec.push_back(tempNode->val);
                if(tempNode->left!=NULL) tempque.push(tempNode->left);//处理左子树
                if(tempNode->right!=NULL) tempque.push(tempNode->right);//处理右子树
            }
            que = tempque;//que赋值为下一层的节点
            ret.push_back(tempVec);//每一层的结果保存下来
        }
        reverse(ret.begin(),ret.end());//反向
        return ret;
    }
};

【一天一道LeetCode】#107. 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 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  10. Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS

    题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...

随机推荐

  1. rasa_core:基于机器学习的对话引擎

    用机器学习管理你的对话,让它提升每一个对话.Rasa Core引导对话,考虑对话的历史和外部环境. 而不是成千上万的规则,Rasa 从真正的对话中挑选模式. 现在是扔掉你的状态机的时候了! Manag ...

  2. IBM-x3650做RAID5更换硬盘后rebuild步骤分享

    1.按Ctrl+H进入WebBIOS配置 2.点击start 3.点击Drives,对slot5进行配置 4.选择slot5,选择Properties,点击Go按钮 5.选择MakeUnconfGoo ...

  3. js将当前时间格式化为年-月-日 时:分:秒

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. include和require的区别比较

    require 的使用方法如 require("MyRequireFile.php"); .这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require ...

  5. Python安装与环境变量

    Python安装与环境变量的配置  python下载: Python安装包下载地址:http://www.python.org/ 根据实际的操作系统,安装合适的安装版本.    Python安装: 本 ...

  6. CSS中display:block属性的作用

    display:block可以理解为块,举个简单的例子!比如你做一个超链接,<li><a href="#">超链接</a></li> ...

  7. ng-book札记——Angular工作方式

    Angular应用由组件(Component)构成.它与AngularJS中的指令相似(directive). 应用 一个Angular应用本质上是一个组件树.在组件树的顶层,最上级的组件即是应用本身 ...

  8. ACM 最小公倍数

    给定两个正整数,计算这两个数的最小公倍数. Input 输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.Output对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行.  ...

  9. Conference-Web Search and Data Mining

    Conference WSDM(Web Search and Data Mining)The ACM WSDM Conference Series 不像KDD.WWW或者SIGIR,WSDM因为从最开 ...

  10. 预处理指令--C语言

    ANSI标准C还定义了如下几个宏: __LINE__ 表示正在编译的文件的行号 __FILE__ 表示正在编译的文件的名字 __DATE__ 表示编译时刻的日期字符串,例如:"25 Dec ...