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

下面我介绍下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. iOS.ReactNative-4-react-native-command-line-tool

    Command line tool: react-native 1. react-native 是一个命令行工具 1.1 react-native简介 运行以下命令: ls -lt `which re ...

  2. each函数

  3. [VS2013]如何闪开安装VS2013必须要有安装IE10的限制

    来源:http://blog.163.com/qimo601@126/blog/static/1582209320143354446462/   已阻止安装程序,此版本的Visual Studio需要 ...

  4. ADB工具常用指令和使用情形分析

    一.ADB常用命令:(不需要进到设备,也就是:不需要执行adb shell) adb devices 列出所有的已连接的设备 adb kill-server 关闭adb adb start-serve ...

  5. js学习-DOM之动态创建元素的三种方式、插入元素、onkeydown与onkeyup两个事件整理

    动态创建元素的三种方式: 第一种: Document.write(); <body> <input type="button" id="btn" ...

  6. 奇葩问题之ToolBar返回键失效

    今天遇到一个奇葩问题,先说现象:接口调用成功,但是在后台业务上报错时(比如:手机号已经被注册过时,接口调用成功,但是后台返回了错误信息:手机号已被注册),toolBar的返回键失效了. 后来仔细看了一 ...

  7. Tomcat7.0安装配置详细

    说明:Tomcat服务器上一个符合J2EE标准的Web服务器,在tomcat中无法运行EJB程序,如果要运行可以选择能够运行EJB程序的容器WebLogic,WebSphere,Jboss等:Tomc ...

  8. 2.每人自己建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令。比较项目的新旧版本的差别。答题人:张立鹏

    第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...

  9. springmvc controller junit 测试

    第一次搭建SSM框架,整合SpringMVC完成后进行Controller测试,找资料并解决问题. 下图是我的完整目录: 1 建立UserController类 代码清单 1-1:UserContro ...

  10. 必填项(required)

    当你设计表单时,你可以指定某些选项为必填项(required),只有当用户填写了该选项后,用户才能够提交表单. 例如,如果你想把一个文本输入字段设置为必填项,在你的input元素中加上required ...