题目:

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

Example

Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

[
[15,7],
[9,20],
[3]
]

题解:

  迭代法,利用队列,不能用栈。

Solution 1 ()

class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if (root == nullptr) {
return res;
}
vector<int> v;
queue<TreeNode*> q;
q.push(root);
q.push(nullptr); while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
if(cur == nullptr) {
res.insert(res.begin(),v);
v.clear();
if(!q.empty()) {
q.push(nullptr);
}
continue;
}
v.push_back(cur->val);
if(cur->left != nullptr) {
q.push(cur->left);
}
if(cur->right != nullptr) {
q.push(cur->right);
}
} return res;
}
};

Solution 1.1 ()

class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if (root == nullptr) {
return res;
}
queue<TreeNode*> q;
q.push(root); while (!q.empty()) {
int size = q.size();
vector<int> v;
for (int i = ; i < size; ++i) {
TreeNode* cur = q.front();
q.pop();
if(cur->left != nullptr) {
q.push(cur->left);
}
if(cur->right != nullptr) {
q.push(cur->right);
}
v.push_back(cur->val);
}
res.insert(res.begin(),v);
}
return res; }
};

  递归

Solution 2 ()

 

【Lintcode】070.Binary Tree Level Order Traversal II的更多相关文章

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

  2. 【Lintcode】069.Binary Tree Level Order Traversal

    题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...

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

  4. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  5. 【easy】107. Binary Tree Level Order Traversal II 按层输出二叉树

    按层输出二叉树,广度优先. 3 / \ 9 20 / \ 15 7 [ [15,7], [9,20], [3] ] /** * Definition for a binary tree node. * ...

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

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

  7. 【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

  8. 【LeetCode】102 - Binary Tree Level Order Traversal

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

  9. 【LeetCode】102. Binary Tree Level Order Traversal 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目描述 Given a bi ...

随机推荐

  1. java操作pdf

    使用pdf模板生成pdf 1,工具 Adobe Acrobat X Pro 2,pom文件配置 <dependency> <groupId>com.itextpdf</g ...

  2. Appium python Uiautomator2 多进程问题

    appium更新uiautomator后可以获取tost了,大家都尝试,课程中也讲解了,但是这些跑的时候都在单机上,当我们多机并发的时候会出现一个端口问题,因为我们appium最后会调用uiautom ...

  3. 目标检测之hog(梯度方向直方图)---hog简介0

    梯度直方图特征(HOG) 是一种对图像局部重叠区域的密集型描述符, 它通过计算局部区域的梯度方向直方图来构成特征.Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功 ...

  4. leetCode 90.Subsets II(子集II) 解题思路和方法

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  5. Project Euler:Problem 41 Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  6. 申请edu邮箱

    https://blog.csdn.net/w305607610/article/details/86771593 手把手教你申请CCC(City Colleges of Chicago)教育邮箱 台 ...

  7. EasyNVR流媒体服务器接入EasyDSS云视频平台快照上传实现

    EasyNVR拥有接入EasyDSS云平台的功能 接入EasyDSS云平台会定时向云平台上传快照数据,这个快照数据用于云平台向客户端提供快照展示 遇到的问题 由于快照上传的间隔提供认为修改的功能,则我 ...

  8. Chrome 的滚动条修改.

    该方法针对于win下Chrome任何版本(未测试基于Chrome内核的其他浏览器),Lunix就是目录换了一下 目录是:**\Google\Chrome\User Data\Profile 2\Use ...

  9. Pentaho BIServer Community Edtion 6.1 使用教程 第四篇 安装和使用Saiku 插件 进行 OLAP

    OLAP(On-Line Analytical Processing,联机分析处理)是一个使分析师.管理者和执行者从原始数据中用来快速.一致.交互访问的一种软件技术,从而真实的反映企业的数据情况.OL ...

  10. 扫盲-wpf依赖属性

    一.什么是依赖属性 依赖属性就是一种自己可以没有值,并且可以通过绑定从其他数据源获取值.依赖属性可支持WPF中的样式设置.数据绑定.继承.动画及默认值. 将所有的属性都设置为依赖属性并不总是正确的解决 ...