Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS
题意是倒过来层次遍历二叉树
下面我介绍下BFS的基本框架,所有的BFS都是这样写的
struct Nodetype {
int d;//层数即遍历深度
KeyType m;//相应的节点值
}
queue<Nodetype> q;
q.push(firstnode);
while(!q.empty()){
Nodetype now = q.front();
q.pop();
........
for(遍历所有now点的相邻点next){
if(!visit[next]) {
访问每个没有访问过的点;
做相应的操作;
next.d = now.d + ;
q.push(next);
}
}
}
对于二叉树将val看成层数,这边我偷懒了。
/**
* 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) { vector<vector<int>> v;
if(!root) return v;
vector<int> t; t.push_back(root->val);
v.push_back(t);
root->val = ; queue<TreeNode*> q;
q.push(root); while(!q.empty()){
TreeNode* now = q.front();
q.pop();
if(!now) continue; q.push(now->left);
q.push(now->right);
if(now->val < v.size()){
if(now->left) v[now->val].push_back(now->left->val);
if(now->right) v[now->val].push_back(now->right->val);
}
else{
vector<int> t;
if(now->left) t.push_back(now->left->val);
if(now->right) t.push_back(now->right->val);
if(t.size() != )v.push_back(t);
}
if(now->left) now->left->val = now->val + ;
if(now->right)now->right->val = now->val + ;
}
reverse(v.begin(),v.end());
return v;
}
};
Leetcode 107 Binary Tree Level Order Traversal II 二叉树+BFS的更多相关文章
- [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 ...
- leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II
相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- 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 ...
- (二叉树 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 ...
- 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 ...
- leetcode 107 Binary Tree Level Order Traversal II ----- java
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- 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 ...
- Java [Leetcode 107]Binary Tree Level Order Traversal II
题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...
随机推荐
- Servlet实现自动刷新功能
使用Servlet实现自动刷新功能,每一秒钟在浏览器输出一个随机数字. package chensi.com; import java.io.IOException; import java.util ...
- Unity3D手游开发日记(8) - 运动残影效果
2D游戏的残影很简单,美术做序列帧图片就行了,那么3D游戏的残影美术做不了,得靠程序员动态创建模型来处理. 实现原理也很简单: 1.间隔一定时间创建一个残影模型 GameObject go = Gam ...
- C/C++语言的一些精简归纳
前言:本想直接写个关于OC语言,但觉得还是要说下C先. 先语言特性 C是面向过程的,没有类和对象概念,也就没有什么封装(这个?).继承.多态等特性. 而且是是中级语言,其编译过程包括:预编译(incl ...
- 程序员遇到Bug时的25个反应
开发应用程序是一个非常有压力的工作.没有人是完美的,因此在这个行业中,代码中出现bug是相当普遍的现象.面对bug,一些程序员会生气,会沮丧,会心烦意乱,甚至会灰心丧气,而另一些程序员会依然保持冷静沉 ...
- FreeBSD_11-系统管理——{Part_2-核心}
一.Managing Services in FreeBSD Service restart will only work if it is set to YES in /etc/rc.conf. T ...
- (Python )运算符
这一节,将学习运算符,主要是算术运算符和逻辑运算符 1.算术运算符 除法运算,整数/整数=整数,浮点数/整数=浮点数,整数/浮点数=浮点数: >>> 17/35>>> ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- select练习1
1. 查询Student表中的所有记录的Sname.Ssex和Class列. select t.sname ,t.ssex , t.sclass from student t 2. 查询教师所有的单位 ...
- Android Studio 中配置强大的版本管理系统
1. 安装Git/CVS 第一步首先你需要安装Git/CVS等版本管理工具,这个请自行百度 2. 新建一个本地空仓库 新建一个仓库叫GitTest 仓库现在是空的什么都没有 找到路径,然后复制路径(这 ...
- WordPaster.exe安装教程
安装教程: Firefox控件安装教程 Chrome控件安装教程 Chrome 45+控件安装教程 相关问题: 提示Runtime Error错误 360拦截 Chrome启用npapi Fire ...