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 ...
随机推荐
- Java写操作
//:ThinkingInJava/net.mindview.io/write2File.java package net.mindview.io; import java.io.BufferedRe ...
- RGBA 与opacity
RGBA是一种表示颜色的方式,初次看到觉得很奇怪,与RGB的区别是什么?后面查了下,才发现RGBA的好处. RGBA各个字母的含义为: R:红色值,正整数 | 百分数: G:绿色值,正整数 | 百分数 ...
- zabbix使用host metadata方式主动注册
host metadata是zabbix2.2新增加的功能,该功能在zabbix-agent端可以自定义条件,在选择自动注册的时候,zabbix-server端可以根据host metadata来选择 ...
- ue4 SNew补遗
上一篇分析了SNew背后的实现,但是有一个关键问题遗漏了,那就是: #define SNew( WidgetType, ... ) \ MakeTDecl<WidgetType>( #Wi ...
- 【转】jquery validate验证框架与kindeditor使用需二次提交的问题
原文:http://blog.csdn.net/wlsyn/article/details/11536399在使用jquery的验证框架对kindeditor富文本编辑器进行验证的过程中,发现提 交时 ...
- CSS定位之position详解(转载)
本文转载于xingoo的博文:http://www.cnblogs.com/xing901022/p/5193751.html position属性 在前端中,position是很常见的属性.通过这 ...
- pxe kickstart 配置+TFTP+NFS多版本系统部署
系统centos6.5x86_64 1.DHCP服务配置. yum安装dncp [root@cnhzdhcp16593 ~]# yum -y install dhcp 配置dhcp服务,新建dhcpd ...
- 黑马程序员+Winform基础(上)
黑马程序员+Winform基础 ---------------<a href="http://edu.csdn.net"target="blank"> ...
- SQL语句 in和inner join各有什么优点
比如A1表 100W行 A2表50W行select a.* from A1 a where a.column1 in (select b.column1 from A2 b where b.colum ...
- Dynamic CRM 2013学习笔记(十二)实现子表合计(汇总,求和)功能的通用插件
上一篇 Dynamic CRM 2013学习笔记(十一)利用Javascript实现子表合计(汇总,求和)功能 , 介绍了如何用js来实现子表合计功能,这种方法要求在各个表单上添加js方法,如果有很多 ...