【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 ...
随机推荐
- tomcat-性能优化参考
转摘 http://blog.csdn.net/lifetragedy/article/details/7708724. ###jdk1.6.未验证.仅供参考### linux环境下Tomcat调优 ...
- 基于window ftp上传问题
FtpClient上传文件异常:java.net.SocketException: Connection reset cmd输入: netsh advfirewall set global State ...
- HTML5 回到顶部
图片: html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...
- 【串线篇】SpringMvc框架乱码
提交的数据可能有乱码: * 请求乱码: * GET请求:改server.xml:在8080端口处URIEncoding="UTF-8" * POST请求: * ...
- malloc,free实验
#include <stdio.h> #include <stdlib.h> int main() { char a = 0; //int * p = (int * ) mal ...
- 【和孩子一起学编程】 python笔记--第三天
第十章 游戏时间:Skier 首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了 代码: import pygame, sys, random skier_ ...
- php wordwrap()函数 语法
php wordwrap()函数 语法 wordwrap()函数怎么用? wordwrap()函数表示按照指定长度对字符串进行折行处理,语法是wordwrap(string,width,break,c ...
- paper 135:关于C#泛型的知识点
计划着要用一个月的时间把 C#语言Windows程序设计 搞定,现在是零零散散的知识点,日积月累吧!朋友们,看这里咯~呵呵 原文地址:http://www.blogjava.net/Jack2007 ...
- Echarts 甘特图教程
Echarts甘特图教程 echarts官网实例: https://gallery.echartsjs.com/editor.html?c=xEYpsVs30s 效果: 代码: <html& ...
- VC++ 字符串操作学习总结
vc++中各种字符串(转载) http://www.cnblogs.com/tomin/archive/2008/12/28/1364097.html CString ,BSTR ,LPCTSTR之间 ...