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). (Easy)

For example:
Given binary tree [3,9,20,null,null,15,7],

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its bottom-up level order traversal as:

  1. [
  2. [15,7],
  3. [9,20],
  4. [3]
  5. ]

分析:

只需要在层序遍历的基础上(参见Binary Tree Level Order Traversal)将最后结果vector reverse即可

代码:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. private:
  12. vector<vector<int>> result;
  13. public:
  14. vector<vector<int>> levelOrderBottom(TreeNode* root) {
  15. if (root == nullptr) {
  16. return result;
  17. }
  18. queue<TreeNode* > que;
  19. que.push(root);
  20. while (!que.empty()) {
  21. int sz = que.size();
  22. vector<int> temp;
  23. for (int i = ; i < sz; ++i) {
  24. TreeNode* cur = que.front();
  25. que.pop();
  26. temp.push_back(cur -> val);
  27. if (cur -> left != nullptr) {
  28. que.push(cur -> left);
  29. }
  30. if (cur -> right != nullptr) {
  31. que.push(cur -> right);
  32. }
  33. }
  34. result.push_back(temp);
  35. }
  36. reverse(result.begin(), result.end());
  37. return result;
  38. }
  39. };

LeetCode107 Binary Tree Level Order Traversal II的更多相关文章

  1. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

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

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

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

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

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

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

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

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

  8. 63. Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...

  9. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

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

随机推荐

  1. JUnit4中@Before、@After、@Test等注解的作用

    转载:https://blog.csdn.net/tn_java/article/details/53837024 JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的 ...

  2. sulin LuceneNet 搜索二

    1.添加引用dll using Lucene.Net.Search;using Lucene.Net.Analysis.PanGu;using PanGu;using PanGu.HighLight; ...

  3. LUOGU P3024 [USACO11OPEN]奶牛跳棋Cow Checkers

    题目描述 One day, Bessie decides to challenge Farmer John to a game of ‘Cow Checkers’. The game is playe ...

  4. 用VUE监听数组和对象的变化

    看一下演示代码,先是增加数组和对象. <template> <div> <p>这是我定义的数组</p> <div>{{this.arr}}& ...

  5. jnhs[未解决]无法使用选定的hibernate配置文件建立数据库连接.请验证hibernate.cfg.xml中的数据库连接详情信息

    工程可以正常的使用读写数据库,当然model和model.hbm.xml文件是自己写的. 解决中

  6. XML解析器之JAXP与DOM4J

    XML是一种数据格式,那么需要对XML文件进行操作就需要用到XML解析器---------针对dom方式和sax方式提供了不同的解析技术-----需要不同的XML解析器 dom方式:会把文档中所有元素 ...

  7. 华为RH2288 V3服务器进入BIOS并设置iBMC地址

    服务器重启后 第一个画面 提示Ctrl+H或Ctrl+S进入相关配置 其中, Ctrl+H进入WebBIOS初次安装系统可在此做RAID配置 此画面不做操作,等待跳过 进入第二画面 可以选择BootM ...

  8. iTerm2+oh-my-zsh配色

      效果图,很帅气有木有 一.首先安装iTem2 安装好后的截图如下:   安装好后的截图 二.安装oh-my-zsh. sh -c "$(curl -fsSL https://raw.gi ...

  9. iOS 动画队列—仿映客刷礼物效果

    http://www.cocoachina.com/ios/20160719/17101.html 最近在研究直播的相关知识,在网上看到了不少优秀的开源项目,但都没有看到映客那个刷礼物的效果,于是手痒 ...

  10. NOIP模拟17.10.12

    T1 临江仙 旧梦 题目背景 闻道故园花陌,今年奼紫嫣红.扬帆直渡水千重.东君何解意,送我一江风. 还是昔时庭院,终得醉卧花丛.残更惊醒月明中.流光如旧岁,多少梦成空. 题目描述 #define go ...