[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;
}
};

最新文章

  1. CSS元素居中的常用方法
  2. 对冲的艺术——delta中性交易
  3. HD1814Peaceful Commission(模板题)
  4. 入门:HTML表单与Java 后台交互(复选框提交)
  5. POJ-A Simple Problem with Integers
  6. C primer plus 练习题 第二章
  7. ①spirngMVC框架运行原理图
  8. FJ省队集训最终测试 T3
  9. spring mvc &amp;lt;mvc:annotation-driven&amp;gt;配置使用出现故障
  10. 安装linux版qq,安装二进制包编译器,安装mysql-5.6.11,删除已安装或安装失败的mysql-5.6.11,简单mysql练习题
  11. jquery在调试时出现缺少对象的错误
  12. 菊花加载第三方--MBprogressHUD
  13. C# 堆栈和堆 Heap &amp; Stack
  14. [转]Request Flow for Provisioning Instance in Openstack
  15. MySQL通过分组计算百分比
  16. Python获取网页指定内容(BeautifulSoup工具的使用方法)
  17. @property专题
  18. DX9 DirectX键盘控制程序 代码
  19. springMVC操作cookie和session
  20. Shiro安全框架学习笔记

热门文章

  1. getopts的错误报告模式
  2. 转载:tomcat过程原理
  3. JAVA中的SimpleDateFormat yyyy和YYYY的区别
  4. Codeforces 1203F (贪心, DP)
  5. react 获取token
  6. C语言之——__attribute__
  7. jquery submit事件
  8. (urls.E006) The MEDIA_URL setting must end with a slash. (urls.e006)
  9. python+appium学习总结
  10. MySQL 时间戳与日期格式的相互转换(转)