层序遍历,使用队列将每层压入,定义两个队列来区分不同的层。

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)的更多相关文章

  1. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  2. C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)

    145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...

  3. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  4. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  5. Java for LeetCode 145 Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  6. leetcode 145. Binary Tree Postorder Traversal ----- java

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  7. leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given bina ...

  8. LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [,,] \ / O ...

  9. leetcode - [6]Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  10. 【leetcode】Binary Tree Postorder Traversal

    题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...

随机推荐

  1. Java实验报告(实验四)

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java    班级:1352班      姓名:王国伊    学号:20135207 成绩:             指导 ...

  2. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

    接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...

  3. 【链表】在O(1)的时间删除链表的节点

    /** * 在O(1)的时间删除链表的节点 * * @author * */ public class Solution { public static void deleteNode(Node he ...

  4. C++派生类继承的理解

    #include<iostream> using namespace std; class Base{ public: Base() { a=; cout<<"Bas ...

  5. 006.C++头文件

    1.引用头文件 标准头文件       #include <iostream> 自定义头文件   #include "complex.h" 2.防卫式(guard)声明 ...

  6. 神经网络CNN训练心得--调参经验

    1.样本要随机化,防止大数据淹没小数据 2.样本要做归一化.关于归一化的好处请参考:为何需要归一化处理3.激活函数要视样本输入选择(多层神经网络一般使用relu)4.mini batch很重要,几百是 ...

  7. scrapy架构设计分析

    scrapy是一个Python爬虫框架.我们自己用requests也能写爬虫(GET某个URL,然后Parse网页的内容),那么,问题来了,scrapy高明在哪些地方呢?下面就来讨论下这个话题,看看业 ...

  8. iOS 隐藏导航栏下的黑线

    一.找到导航栏下的黑线 // 寻找导航栏下的黑线 - (UIImageView *)findHairlineImageViewUnder:(UIView *)view { if ([view isKi ...

  9. codeforces contest 864 problemD

    Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently ...

  10. python初步学习-pycharm使用

    Pycharm简介 PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成. ...