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 to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7},
return its bottom-up level order traversal as:
分析
与LeetCode(103) Binary Tree Zigzag Level Order Traversal 以及 LeetCode(102) Binary Tree Level Order Traversal 本质相同的题目,只不过灵活调整结果返回格式罢了。
AC代码
/**
* 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) {
//层次遍历,分层存储
if (!root)
return vector<vector<int> >();
vector<vector<int> > ret;
//定义两个队列,一个存储所有的父节点,另一个存储他们的子节点也就是子层
queue<TreeNode *> parents;
parents.push(root);
while (!parents.empty())
{
//存储当前层的遍历结果
vector<int> tmp;
//定义队列存储他们的子节点也就是子层
queue<TreeNode *> childs;
while (!parents.empty())
{
TreeNode *node = parents.front();
tmp.push_back(node->val);
//弹出当前父节点
parents.pop();
if (node->left)
childs.push(node->left);
if (node->right)
childs.push(node->right);
}
//存储当前层的遍历结果
ret.push_back(tmp);
//遍历下一层
parents = childs;
}
//反转遍历结果 由下向上存储
reverse(ret.begin(), ret.end());
return ret;
}
};
LeetCode(107) Binary Tree Level Order Traversal II的更多相关文章
- LeetCode(26)-Binary Tree Level Order Traversal II
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- LeetCode(102) Binary Tree Level Order Traversal
题目 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to rig ...
- 【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal ...
- LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal o ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 35. Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/binary-tree-level-order-trave ...
- LeetCode_107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II Easy Given a binary tree, return the bottom-up level order ...
- Binary Tree Level Order Traversal,Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal Total Accepted: 79463 Total Submissions: 259292 Difficulty: Easy G ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
随机推荐
- UVa12298(生成函数的简单应用+FFT)
I have a set of super poker cards, consisting of an infinite number of cards. For each positive compo ...
- Problem D. What a Beautiful Lake dp
Problem D. What a Beautiful Lake Description Weiming Lake, also named "Un-named Lake", is ...
- ruby 正则匹配返回值matchdata
引用连接: 为处理与正则表达式的匹配过程相关的信息而设置的类. 可以通过下列途径 Regexp.last_match Regexp#match, String#match $~ 得到该类的实例. 超类 ...
- this详解,对执行上下文说 Yes
this 指向多变,很多隐蔽的 bug 都缘于它.与此同时,this 强大灵活,如果能熟练驾驭,就会写出更简洁.优雅的代码. 社区上对于 this 的讲解虽然不少,但缺乏统一梳理. this 相关知识 ...
- uvm_pkg——老板,打包带走
Thus spake the master programmer: “After three day without programming, life becomes meaningless.” 编 ...
- [Python]输出中文报错的解决方法
问题现象:在PyCharm工具编辑python语句输出中文时,程序报错. 解决方法(2种): 1.在代码开头加#coding=utf-8(注意要加#) 2.还是在代码开头加#-*- coding: u ...
- MyBatis插入数据之后返回插入记录的id
MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...
- jquery 不支持$.browser
if (!$.browser) { $.browser = { mozilla : /firefox/.test(navigator.userAgent.toLowerCase()), webkit ...
- ubuntu 14.04 安装tomcat服务器 配置图片路径和文件路径
root@hett-PowerEdge-T30:/usr/local/src# lltotal 235956drwxr-xr-x 6 root root 4096 3月 26 14:48 ...
- Java动态代理之InvocationHandler最简单的入门教程
网上关于Java的动态代理,Proxy和InvocationHandler这些概念有讲解得非常高深的文章.其实这些概念没有那么复杂.现在咱们通过一个最简单的例子认识什么是InvocationHandl ...