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 ...
随机推荐
- Selenium2+python自动化23-富文本(自动发帖)
前言 富文本编辑框是做web自动化最常见的场景,有很多小伙伴遇到了不知道无从下手,本篇以博客园的编辑器为例,解决如何定位富文本,输入文本内容 一.加载配置 1.打开博客园写随笔,首先需要登录,这里为了 ...
- iOS获取当前app的名称和版本号
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...
- linux中的开机和关机命令
与关机.重新启动相关的命令 * 将数据同步写入硬盘中的命令 sync * 惯用的关机命令 shutdown * 重新启动.关机 reboot halt poweroff sync 强制将内存中的 ...
- as 和 is 区别
as 将对象转换某类型,如果失败则返回null.is用来判断对象是否为某个类型,在判断过程中会对对象进行两次转换,而as只有一次转换,故as效率高.
- [付费视频]Delphi视频Android开发使用静态库(A)和动态库(SO)
关于本视频:前阵子接到一个委托,解决Delphi开发Android程序中串口通信的问题,厂家那边提供了c文件,需要翻译成delphi可用,翻译倒是比较简单.不过后来翻译读写ic卡单元的时候进行不下去了 ...
- oracle删除数据恢复
分为两种方法:scn和时间戳两种方法恢复. 一.通过scn恢复删除且已提交的数据 1.获得当前数据库的scn号 select current_scn from v$database; (切换到sys用 ...
- Tomcat需要更改三个端口,才能在一台机器上搭载多个tomcat
1.shutdown端口.port改成与原有的不同即可 2.8080端口,需要更改. 3.默认端口
- Keepalived安装配置
一. 介绍 keepalived:是一个类似于 layer3, 4 & 7 交换机制的软件,也就是我们平时说的第 3 层.第 4 层和第 7层交换. Keepalived 的作用是检测 we ...
- Windows 8.1 应用再出发 - 几种新增控件(1)
Windows 8.1 新增的一些控件,分别是:AppBar.CommandBar.DatePicker.TimePicker.Flyout.MenuFlyout.SettingsFlyout.Hub ...
- JAVA学习博客---2015.5
上一次的学习博客写的和流水账差不多,有点生硬的和背目录一样,所以既然学习的目的是程序,那么这个月的学习博客就用程序来说点东西吧.这个月看了一些C和JAVA的视频,开始看别人写的程序,能看的懂但是自己去 ...