目录
一、N叉树的层序遍历
广度优先搜索
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
vector<vector<int>> levelOrder(Node* root) {
if(root == NULL) return {};
vector<vector<int>> ans;
queue<Node*> queue;
queue.push(root);
while(!queue.empty()){
int count = queue.size();
vector<int> flag;
for(int i = 0; i < count; i++){
Node* node = queue.front();
queue.pop();
flag.push_back(node->val);
for(Node* child: node->children){
queue.push(child);
}
}
ans.push_back(flag);
}
return ans;
}
};
二、等差子数组
暴力
class Solution {
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
int n = l.size();
vector<bool> ans;
for(int i = 0; i < n; i++){
vector<int> flag;
for(int m = l[i]; m <= r[i]; m++)
flag.push_back(nums[m]);
sort(flag.begin(), flag.end());
bool isTrue = true;
int distance = flag[1] - flag[0];
for(int m = 2; m < flag.size(); m++){
if(flag[m] - flag[m - 1] != distance){
isTrue = false;
break;
}
}
ans.push_back(isTrue);
}
return ans;
}
};