按层输出二叉树,广度优先。

    3
/ \
9 20
/ \
15 7
[
[15,7],
[9,20],
[3]
]
/**
* 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>> rres; void levelTree(TreeNode*root, int level){
if (root == NULL)
return;
if (level == rres.size()){
vector<int> res;
rres.push_back(res);
} rres[level].push_back(root->val);
levelTree(root->left,level+);
levelTree(root->right,level+);
} vector<vector<int>> levelOrderBottom(TreeNode* root) {
levelTree(root,);
//return reverse(rres.begin(),rres.end());
return vector<vector<int> >(rres.rbegin(), rres.rend());
}
};

【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树的更多相关文章

  1. 102/107. Binary Tree Level Order Traversal/II

    原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...

  2. 【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 ...

  3. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  4. (二叉树 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 ...

  5. 【Leetcode】【Easy】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  6. 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 ...

  7. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

  8. [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 ...

  9. 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)

    从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...

随机推荐

  1. PHP开发高可用高安全App后端

    基于thinkphp5开发的APP,涵盖阿里大于,七牛云图片上传,RestfulApi,短信验证, 需要联系我:QQ:1844912514

  2. ftm国际化解决方案

    记录一下踩过的坑,在使用ftm:message的时候我发现这个的国际化是依赖于本地浏览器的语言环境的!关于自主设置这个语言的方法有如下3种:(个人建议使用第二种,可以更加灵活且有效!第一种我这边没有生 ...

  3. [洛谷P1392] 取数

    无法用复杂状态进行转移时改变计算方式:巧妙的整体考虑:压缩空间优化时间 传送门:$>here<$ 题意 给出一个n*m矩阵,从每一行选一个数加起来,可以得到一个和.易知总共会有$n^n$个 ...

  4. CF1114D 【Flood Fill】

    Solution 一看就是很水的区间DP \(dp[i][j]\)表示区间\([l,r]\)都涂成同色的代价. \(dp[i][j] = min( dp[i][j], dp[i][k] + dp[k] ...

  5. 洛谷P3327 约数个数和 结论+莫比乌斯反演

    原题 就是让你求\(\sum\limits_{i=1}\sum\limits_{j=1}d(ij)\)(其中\(d(x)\)表示\(x\)的因数个数) 首先有引理(然而并没有证明): \(d(ij)= ...

  6. jacoco+maven 初次使用覆盖率工具

    工作要搞覆盖率测试,看到公司平台上用的jacoco,就找了网上的demo自己跑了一下. 一.覆盖率测试是干什么的 http://www.open-open.com/lib/view/open14721 ...

  7. Python菜鸟快乐游戏编程_pygame(5)

    Python菜鸟快乐游戏编程_pygame(博主录制,2K分辨率,超高清) https://study.163.com/course/courseMain.htm?courseId=100618802 ...

  8. 状压dp(状态压缩&&dp结合)学习笔记(持续更新)

    嗯,作为一只蒟蒻,今天再次学习了状压dp(学习借鉴的博客) 但是,依旧懵逼·································· 这篇学习笔记是我个人对于状压dp的理解,如果有什么不对的 ...

  9. 编写高质量的Python代码系列(八)之部署

    Python提供了一些工具,使我们可以把软件部署到不同的环境中.它也提供了一些模块,令开发者可以把程序编写的更加健壮.本章讲解如何使用Python调试.优化并测试程序,以提升其质量与性能. 第五十四条 ...

  10. Angular记录(10)

    文档资料 速查表:https://www.angular.cn/guide/cheatsheet 风格指南:https://www.angular.cn/guide/styleguide Angula ...