LeetCode107 Binary Tree Level Order Traversal II
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]
,
- 3
- / \
- 9 20
- / \
- 15 7
return its bottom-up level order traversal as:
- [
- [15,7],
- [9,20],
- [3]
- ]
分析:
只需要在层序遍历的基础上(参见Binary Tree Level Order Traversal)将最后结果vector reverse即可
代码:
- /**
- * 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 {
- private:
- vector<vector<int>> result;
- public:
- vector<vector<int>> levelOrderBottom(TreeNode* root) {
- if (root == nullptr) {
- return result;
- }
- queue<TreeNode* > que;
- que.push(root);
- while (!que.empty()) {
- int sz = que.size();
- vector<int> temp;
- for (int i = ; i < sz; ++i) {
- TreeNode* cur = que.front();
- que.pop();
- temp.push_back(cur -> val);
- if (cur -> left != nullptr) {
- que.push(cur -> left);
- }
- if (cur -> right != nullptr) {
- que.push(cur -> right);
- }
- }
- result.push_back(temp);
- }
- reverse(result.begin(), result.end());
- return result;
- }
- };
LeetCode107 Binary Tree Level Order Traversal II的更多相关文章
- [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- 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 ...
- 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 ...
- 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 ...
- 102/107. Binary Tree Level Order Traversal/II
原文题目: 102. Binary Tree Level Order Traversal 107. Binary Tree Level Order Traversal II 读题: 102. 层序遍历 ...
- 【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 ...
- 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 ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- [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 ...
随机推荐
- JUnit4中@Before、@After、@Test等注解的作用
转载:https://blog.csdn.net/tn_java/article/details/53837024 JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的 ...
- sulin LuceneNet 搜索二
1.添加引用dll using Lucene.Net.Search;using Lucene.Net.Analysis.PanGu;using PanGu;using PanGu.HighLight; ...
- LUOGU P3024 [USACO11OPEN]奶牛跳棋Cow Checkers
题目描述 One day, Bessie decides to challenge Farmer John to a game of ‘Cow Checkers’. The game is playe ...
- 用VUE监听数组和对象的变化
看一下演示代码,先是增加数组和对象. <template> <div> <p>这是我定义的数组</p> <div>{{this.arr}}& ...
- jnhs[未解决]无法使用选定的hibernate配置文件建立数据库连接.请验证hibernate.cfg.xml中的数据库连接详情信息
工程可以正常的使用读写数据库,当然model和model.hbm.xml文件是自己写的. 解决中
- XML解析器之JAXP与DOM4J
XML是一种数据格式,那么需要对XML文件进行操作就需要用到XML解析器---------针对dom方式和sax方式提供了不同的解析技术-----需要不同的XML解析器 dom方式:会把文档中所有元素 ...
- 华为RH2288 V3服务器进入BIOS并设置iBMC地址
服务器重启后 第一个画面 提示Ctrl+H或Ctrl+S进入相关配置 其中, Ctrl+H进入WebBIOS初次安装系统可在此做RAID配置 此画面不做操作,等待跳过 进入第二画面 可以选择BootM ...
- iTerm2+oh-my-zsh配色
效果图,很帅气有木有 一.首先安装iTem2 安装好后的截图如下: 安装好后的截图 二.安装oh-my-zsh. sh -c "$(curl -fsSL https://raw.gi ...
- iOS 动画队列—仿映客刷礼物效果
http://www.cocoachina.com/ios/20160719/17101.html 最近在研究直播的相关知识,在网上看到了不少优秀的开源项目,但都没有看到映客那个刷礼物的效果,于是手痒 ...
- NOIP模拟17.10.12
T1 临江仙 旧梦 题目背景 闻道故园花陌,今年奼紫嫣红.扬帆直渡水千重.东君何解意,送我一江风. 还是昔时庭院,终得醉卧花丛.残更惊醒月明中.流光如旧岁,多少梦成空. 题目描述 #define go ...