107. Binary Tree Level Order Traversal II(Tree, WFS)
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).
For 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]
]
思路:使用两个队列完成层次搜索,结果存储在vector中。
若使用一个队列,也可完成层次遍历,只是不清楚遍历到哪个层次了。两个队列可以知道目前元素所在的层次。
/**
* 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>> levelOrderBottom(TreeNode* root) {
result.clear();
if(!root) return result; vector<int> level;
TreeNode * current;
queue<TreeNode * > queToPop;
queue<TreeNode * > queToPush;
queToPop.push(root);
while(!queToPop.empty())
{
while(!queToPop.empty())
{
current = queToPop.front();
queToPop.pop();
level.push_back(current->val);
if(current->left)
{
queToPush.push(current->left); //若使用一个队列,这里应该是queToPop.push(...),只要queToPop不为空,就继续pop和push
}
if(current->right)
{
queToPush.push(current->right);
}
} result.push_back(level);
level.clear();
swap(queToPop, queToPush);
} reverse(result.begin(), result.end()); //逆序vector的方法
return result;
}
private:
vector<vector<int>> result;
};
107. Binary Tree Level Order Traversal II(Tree, WFS)的更多相关文章
- 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 ...
- 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 ...
- 63. Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal II My Submissions QuestionEditorial Solution Total Accepted: 79742 ...
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- Webroot SecureAnywhere AntiVirus 2014 – 免费6个月
Webroot SecureAnywhere 是由webroot推出的一款云安全软件,除了能够清除病毒外,特点是体积小.强力查杀木马.间谍软件.Rootkit 等等,为你的个人私隐信息提供全面的保护. ...
- 使用c++实现一个FTP客户端(一)
之前使用c++实现了一个FTP客户端,在这里做一些记录. 一.需要注意的几点 ①FTP是一种文件传输协议,基于TCP,所以客户端与服务器建立的连接是可靠.安全的,并且要经过三次握手的过程. ②FTP传 ...
- Entity Framework 入门
Entity Framework的全称是ADO.NET Entity Framework,是微软开发的基于ADO.NET的ORM(Object/Relational Mapping)框架. Entit ...
- CUDA Samples: matrix multiplication(C = A * B)
以下CUDA sample是分别用C++和CUDA实现的两矩阵相乘运算code即C= A*B,CUDA中包含了两种核函数的实现方法,第一种方法来自于CUDA Samples\v8.0\0_Simple ...
- iOS语法糖 简单却不那么简单
转载作者 香蕉大大 (Github) 开发过程中我特别喜欢用语法糖,原因很简单,懒得看到一堆长长的代码,但是语法糖我今天无意中看到更有意思的玩法.这里暂时吧把今天新学到的知识点整理一下希望大家喜欢,如 ...
- 最新ICE源码编译安装
发现ICE3.7版本在编译安装时比之前的版本省事了很多,少了很多杂七杂八的依赖库:估计是被grpc火热情景给逼的? Compiling [amd64-shared] src/Ice/Collocate ...
- Java中private、protected、public和default的区别 (转)
本文内容转载自: https://www.cnblogs.com/jingmengxintang/p/5898900.html public: 具有最大的访问权限,可以访问任何一个在classpath ...
- Android GPS GPSBasics project hacking
一.参考源码: GPS Basic Example - Android Example http://androidexample.com/GPS_Basic__-__Android_Example/ ...
- 浅谈SQL Server---1
浅谈SQL Server优化要点 https://www.cnblogs.com/wangjiming/p/10123887.html 1.SQL Server 体系结构由哪几部分组成? 2.SQL ...
- java局部变量和临时变量
局部变量:temp=1, 临时变量:return a+b 临时变量会有一点的性能优势 局部变量会比成员变量和静态成员变量有优势,改进的方法是吧成员变量和静态成员变量赋值在局部变量:https://bl ...