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 ...
随机推荐
- 对于iOS性能优化的一点看法
在我们通常的开发工作中,每次需求定下来的时候,开发时间都是很紧张的,于是我们就抓紧时间开发,完成需求.在匆忙开发的过程中,或多或少的会有一些性能问题存在,在开发任务完成以后,我们都要进行性能优化.现将 ...
- bzoj2431: [HAOI2009]逆序对数列(DP)
f[i][j]前i个数有j个逆序对的数量 f[i][j]=sigma(f[i-1][j-k]){1<=k<=i} 维护一个前缀和即可 #include<iostream> #i ...
- ucenter通信实现同步登录、同步退出(详细)
首先,需要去官网下载一个ucenter的包.然后解压下来. 先把ucenter/ucenter这个文件夹复制到你的项目根目录下改名为uc_server;(这里只是我建议修改,以便于我下面写的配置); ...
- kubernetes创建资源对象yaml文件例子--pod详解
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...
- springboot-为内置tomcat设置虚拟目录
需求 项目使用springboot开发,以jar包方式部署.项目中文件上传均保存到D判断下的upload目录下. 在浏览器中输入http://localhost:8080/upload/logo_1. ...
- LightOJ 1218 概率水题(几何分布)
题意:给你一个n面骰子,问你投出所有面需要的次数的期望值是多少. 题解:放在过去估计秒解,结果现在自己想好久,还查了下,有人用极限证明...实际上仔细想想这种情况投出与前面不一样的概率p的倒数就是次数 ...
- WCF使用注意事项
执行如下 批处理:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\svcutil.exe" http://127.0.0.1: ...
- gdoi2017
今年的gdoi第一天t1大水题一道 裸的kmp 但是 我把记录长度的int数组开成了char类型 正解变爆零 心态爆炸......... 后面的第二题两千字题目以及五千字附加故事(我是没有去看,据说全 ...
- 20151024_001_C#基础知识(静态与非静态的区别,值类型和引用类型,堆和栈的区别,字符串的不可变性,命名空间)
1:我们把这些具有相同属性和相同方法的对象进行进一步的封装,抽象出来类这个概念. 类就是个模子,确定了对象应该具有的属性和方法. 对象是根据类创建出来的. 2:类:语法 [public] class ...
- pdf文件添加到word中
今天遇到了一个问题,如何把pdf文件添加到word中,而不是只添加图标,下面是解决方案: 1.用word 打开pdf文件: 2.打开word文件: 3.把1中的pdf文件复制粘贴 到2中的word文件 ...