Leetcode 之Binary Tree Postorder Traversal(45)
层序遍历,使用队列将每层压入,定义两个队列来区分不同的层。
vector<vector<int>> levelorderTraversal(TreeNode *root)
{
vector<vector<int>> result;
vector<int>tmp;
//通过两个queue来区分不同的层次
queue<TreeNode *>current,next;
TreeNode *p = root;
current.push(p);
while (!current.empty())
{
while (!current.empty())
{
p = current.front();
current.pop();
tmp.push_back(p->val);
if (p->left != nullptr)next.push(p->left);
if (p->right != nullptr)next.push(p->right);
}
result.push_back(tmp);
tmp.clear();
//交换并将next清空,
swap(current, next);
} return result;
}
Leetcode 之Binary Tree Postorder Traversal(45)的更多相关文章
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- Java for LeetCode 145 Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- leetcode 145. Binary Tree Postorder Traversal ----- java
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...
- LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...
- leetcode - [6]Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- 【leetcode】Binary Tree Postorder Traversal
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
随机推荐
- LUOGU 1440
#include<cstdio> #include<algorithm> #include<cstring> #define N 1000005 using nam ...
- HDU 1596 floyd
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1814 2-sat 模板
Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Tomcat设置开启时自动访问某个servlet类存在的问题
<servlet> <servlet-name>****</servlet-name> <servlet-class>****</servlet- ...
- Small Multiple
题目描述 Find the smallest possible sum of the digits in the decimal notation of a positive multiple of ...
- 鸽巢排序Pigeonhole sort
原理类似桶排序,同样需要一个很大的鸽巢[桶排序里管这个叫桶,名字无所谓了] 鸽巢其实就是数组啦,数组的索引位置就表示值,该索引位置的值表示出现次数,如果全部为1次或0次那就是桶排序 例如 var pi ...
- 【BZOJ】2679: [Usaco2012 Open]Balanced Cow Subsets
[算法]折半搜索+数学计数 [题意]给定n个数(n<=20),定义一种方案为选择若干个数,这些数可以分成两个和相等的集合(不同划分方式算一种),求方案数(数字不同即方案不同). [题解] 考虑直 ...
- Zabbix 通过 JMX 监控 java 进程
参考: [ JMX monitoring ] [ Zabbix Java gateway ] [ JMX Monitoring (Java Gateway) not Working ] [ Monit ...
- VS推荐插件
以下插件均可在NuGet下载 Smooth Scroll 平滑滚动 Format document on Save 保存时自动格式化代码 Supercharger VS增强插件[破解教程] HideM ...
- TDD随想录
TDD随想录 谨以本文献给TDD的开创者与传播者 本文纯属个人经历,如有雷同纯属巧合 我从不觉得自己是一个好的程序员,甚至可能连合格都谈不上,不过在内心深处我却渴望着在编程这件事上获得成功. 可惜每次 ...