【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道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的更多相关文章
- 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 ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- [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 ...
- (二叉树 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS
题意是倒过来层次遍历二叉树 下面我介绍下BFS的基本框架,所有的BFS都是这样写的 struct Nodetype { int d;//层数即遍历深度 KeyType m;//相应的节点值 } que ...
随机推荐
- 谈谈如何选择合适的MySQL数据类型
MySQL数据类型选择 一 .选择原则 更小的通常更好:一般情况下选择可以正确存储数据的最小数据类型.越小的数据类型通常更快,占用磁盘,内存和CPU缓存更小. 简单就好:简单的数据类型的操作通常需要更 ...
- 触发事件trigger
触发事件,在本例中,文档加载好之后,就触发dblclick双击事件,而不是通过去手动双击. <script src="http://how2j.cn/study/jquery.min. ...
- BookNote: Refactoring - Improving the Design of Existing Code
BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...
- webstorm 设置uglify 压缩js文件
第一步:打开npm,全局安装 npm install uglify-js -g 第二步: 打开webstorm的file->settings ->External Tools,点击左上角的 ...
- Git/GitHub SSH配置
生成 SSH 公钥 如前所述,许多 Git 服务器都使用 SSH 公钥进行认证. 为了向 Git 服务器提供 SSH 公钥,如果某系统用户尚未拥有密钥,必须事先为其生成一份. 这个过程在所有操作系统上 ...
- 谷歌刚发布的求梯度的工具包-Tangent
安装很简单sudo pip install tangent. 我机器上,终端上用python,tangent报错,但在终端上用ipython,tangent不报错. 我检验是否可用tangent的方法 ...
- Node.js 调试器
稳定性: 3 - 稳定 V8 提供了强大的调试工具,可以通过 TCP protocol 从外部访问.Node 内置这个调试工具客户端.要使用这个调试器,以debug参数启动 Node,出现提示: % ...
- Node.js 加密
稳定性: 2 - 不稳定; 正在讨论未来版本的 API 改进,会尽量减少重大变化.详见后文. 使用 require('crypto') 来访问这个模块. 加密模块提供了 HTTP 或 HTTPS 连接 ...
- Android源码解析——Toast
简介 Toast是一种向用户快速展示少量信息的视图.当它显示时,它会浮在整个应用层的上面,并且不会获取到焦点.它的设计思想是能够向用户展示些信息,但又能尽量不显得唐突.本篇我们来研读一下Toast的源 ...
- synchronized与条件同步
在并发编程中,有这样的需求:当满足某个条件时线程执行同步块中的代码,条件不满足时,让线程在此等待,直至条件满足再执行同步代码块. java的Object类即提供了一类这样的方法wait(),notif ...