【LeetCode】BFS || DFS [2017.04.10--2017.04.17]
[102] Binary Tree Level Order Traversal [Medium-Easy]
[107] Binary Tree Level Order Traversal II [Medium-Easy]
这俩题没啥区别。都是二叉树层级遍历。BFS做。
可以用一个队列或者两个队列实现。我觉得一个队列实现的更好。(一个队列实现的重点是利用队列的size来看当前层级的节点数)
/**
* 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>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (!root) {
return ans;
}
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
size_t sz = q.size();
vector<int> level;
for (auto i = ; i < sz; ++i) {
TreeNode* cur = q.front();
q.pop();
level.push_back(cur->val);
if (cur->left) {
q.push(cur->left);
}
if (cur->right) {
q.push(cur->right);
}
}
ans.push_back(level);
}
return ans;
}
};
[101] Symmetric Tree [Easy]
判断一棵树是不是对称树。为啥还是不能一下子就写出来。。。
/**
* 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:
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode* left, TreeNode* right) {
if (!left && !right) {
return true;
}
else if (left == nullptr || right == nullptr) {
return false;
}
else if (left->val != right->val){
return false;
}
return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left);
} };
[542] 01 Matrix [Medium]
给个01矩阵,找到每个cell到 0 entry的最短距离。
从第一个0元素开始BFS, 用队列记录每个 0 元素的位置, 遍历队列,更新一个周围的元素之后,把更新的元素也进队。
队列一定是越来越短的,因为你每pop一个出来,需要更新元素才能push
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
vector<vector<int>> ans;
//const int m[4] = {1, 0, -1, 0}; //top - buttom
//const int n[4] = {0, 1, 0, -1}; //left - right
const vector<pair<int, int>> dir{{, }, {, }, {-, }, {, -}};
if (matrix.size() == || matrix[].size() == ) {
return ans;
}
const int rows = matrix.size(), cols = matrix[].size();
queue<pair<int, int>> q;
for (size_t i = ; i < rows; ++i) {
for (size_t j = ; j < cols; ++j) {
if (matrix[i][j] != ) {
matrix[i][j] = INT_MAX;
}
else {
q.push(make_pair(i, j));
}
}
}
while (!q.empty()) {
pair<int, int> xy = q.front();
q.pop();
for(auto ele: dir) {
int new_x = xy.first + ele.first, new_y = xy.second + ele.second;
if (new_x >= && new_x < rows && new_y >= && new_y < cols) {
if (matrix[new_x][new_y] > matrix[xy.first][xy.second] + ) {
matrix[new_x][new_y] = matrix[xy.first][xy.second] + ;
q.push({new_x, new_y});
}
}
}
}
return matrix;
}
};
【LeetCode】BFS || DFS [2017.04.10--2017.04.17]的更多相关文章
- 【LeetCode】BFS(共43题)
[101]Symmetric Tree 判断一棵树是不是对称. 题解:直接递归判断了,感觉和bfs没有什么强联系,当然如果你一定要用queue改写的话,勉强也能算bfs. // 这个题目的重点是 比较 ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】将罗马数字转换成10进制数
Roman to Integer Given a roman numeral, convert it to an integer. 首先介绍罗马数字 罗马数字共有七个,即I(1),V(5),X(10) ...
- 【LeetCode】BFS 总结
BFS(广度优先搜索) 常用来解决最短路径问题. 第一次便利到目的节点时,所经过的路径是最短路径. 几个要点: 只能用来求解无权图的最短路径问题 队列:用来存储每一层便利得到的节点 标记:对于遍历过的 ...
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- for循环(C语言型)举例
- jquey弹出框demo
默认 $('#btn-01').click(function(){ $.dialog({ contentHtml : '<p>我是默认弹出对话框示例展示.我只是用来占位的内容展示,仅仅用来 ...
- 离线安装gcc_rpm(centos下安装gcc的方法之一)
.解压gcc_rpm.tar.gz (我的CSDN账号下载过) tar -zxvf gcc_rpm.tar.gz .解压完进入文件夹,执行以下命令,挨个执行(如果报依赖错误,就加上"--fo ...
- python 环境安装说明
一. 安装 python-3.6.5-amd64.exe 1. 安装完成后,设置系统环境变量 Path E:\Programs\Python\Python36; E:\Program ...
- spring mvc 整合 druid
环境: ubuntu eclipse maven 一. pom.xml 加入druid 依赖 <!-- https://mvnrepository.com/artifact/com.alibab ...
- Tensorflow中的图(tf.Graph)和会话(tf.Session)详解
Tensorflow中的图(tf.Graph)和会话(tf.Session) Tensorflow编程系统 Tensorflow工具或者说深度学习本身就是一个连贯紧密的系统.一般的系统是一个自治独立的 ...
- 卸载Anaconda
conda install anaconda-clean anaconda-clean --yes Anaconda 安装的时候在 .bash_profile中添加了变量: export PATH=& ...
- beautifhulsoup4的使用
Beautiful: - 基本使用 from bs4 import BeautifulSoup - 解析器: lxml, html.parser soup = Beautiful ...
- Django基础--Django基本命令、路由配置系统(URLconf)、编写视图、Template、数据库与ORM
web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构. 使用框架可以帮你快速开发特定的系统. 简单地说,就是你用别人搭建好的舞台来做表演. 尝试搭建一个简单 ...
- 浏览器地址栏运行HTML代码(谷歌)
在地址栏输入 data:text/html,<h1 style='color:red' >Hello, world!</h1> 浏览器会执行你的html代码,效果如下: 如果觉 ...