【LeetCode】BFS || DFS [2017.04.10--2017.04.17]
2024-09-03 22:24:42
[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;
}
};
最新文章
- CSS元素居中的常用方法
- 对冲的艺术——delta中性交易
- HD1814Peaceful Commission(模板题)
- 入门:HTML表单与Java 后台交互(复选框提交)
- POJ-A Simple Problem with Integers
- C primer plus 练习题 第二章
- ①spirngMVC框架运行原理图
- FJ省队集训最终测试 T3
- spring mvc &;lt;mvc:annotation-driven&;gt;配置使用出现故障
- 安装linux版qq,安装二进制包编译器,安装mysql-5.6.11,删除已安装或安装失败的mysql-5.6.11,简单mysql练习题
- jquery在调试时出现缺少对象的错误
- 菊花加载第三方--MBprogressHUD
- C# 堆栈和堆 Heap &; Stack
- [转]Request Flow for Provisioning Instance in Openstack
- MySQL通过分组计算百分比
- Python获取网页指定内容(BeautifulSoup工具的使用方法)
- @property专题
- DX9 DirectX键盘控制程序 代码
- springMVC操作cookie和session
- Shiro安全框架学习笔记
热门文章
- getopts的错误报告模式
- 转载:tomcat过程原理
- JAVA中的SimpleDateFormat yyyy和YYYY的区别
- Codeforces 1203F (贪心, DP)
- react 获取token
- C语言之——__attribute__
- jquery submit事件
- (urls.E006) The MEDIA_URL setting must end with a slash. (urls.e006)
- python+appium学习总结
- MySQL 时间戳与日期格式的相互转换(转)