题意是倒过来层次遍历二叉树

下面我介绍下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的更多相关文章

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

  2. leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目: 102 103 107 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  3. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

  5. (二叉树 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 ...

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

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

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

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

随机推荐

  1. python 小知识

    PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找. 使用下面的代码可以打印PYTHONPATH: print(os.sys.path) impr ...

  2. P1-概率论基础(Primer on Probability Theory)

    2.1概率密度函数 2.1.1定义 设p(x)为随机变量x在区间[a,b]的概率密度函数,p(x)是一个非负函数,且满足 注意概率与概率密度函数的区别. 概率是在概率密度函数下对应区域的面积,如上图右 ...

  3. C# 字符串替换Replace

    C# 中的石strA.Replace(strB,strC)函数可以实现将strA中的strB替换为strC. 但是容易出错的地方是,这并不是就直接替换好了,此函数的返回值才是替换好的字符串,所以还要要 ...

  4. Ubuntu下修改hosts文件

    Ubuntu系统的Hosts只需修改/etc/hosts文件,修改完之后要重启网络.具体过程如下:1.修改hostssudo gedit /etc/hosts2.添加解析记录( . )或者从githu ...

  5. span width无效

    在默认情况下label.span 设置width 是无效的.一般要display属性 display:block; 但是他会自动加一个换行,如果不想换行的话,可以用 display:inline-bl ...

  6. python基础整理笔记(四)

    一. python 打开文件的方法 1. python中使用open函数打开文件,需要设定的参数包括文件的路径和打开的模式.示例如下: f = open('a.txt', 'r+') 2. f为打开文 ...

  7. testng.xml顺序执行多个case配置

    testng.xml顺序执行多个case配置 项目结构如图:

  8. CentOS6.5 解压安装 二进制分发版 mysql-5.5.49-linux2.6-x86_64.tar.gz

    环境:CentOS 6.5 64位 1.下载安装包 http://dev.mysql.com/downloads/mysql/5.5.html#downloads http://dev.mysql.c ...

  9. UIAlertView、 UIActionSheet

    一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...

  10. oracle11g数据库的安装以及安装之后的配置

    1.按照正常的顺序进行安装,然后安装完成后可以对用户进行管理,(设置sys  sysmanager的口令,激活scott的用户并设置新密码) 这一步没什么好多的,关键是进行安装完成之后的配置,这个就比 ...