Skip to content

Commit 286cc79

Browse files
author
xwk1246
committed
push all
1 parent bbe9cdd commit 286cc79

5 files changed

+207
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <vector>
2+
#include <queue>
3+
using namespace std;
4+
struct TreeNode {
5+
int val;
6+
TreeNode* left;
7+
TreeNode* right;
8+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
9+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
10+
TreeNode(int x, TreeNode l, TreeNode r) :val(x), left(l), right(r) {}
11+
};
12+
/*
13+
* @lc app=leetcode id=102 lang=cpp
14+
*
15+
* [102] Binary Tree Level Order Traversal
16+
*/
17+
18+
// @lc code=start
19+
/**
20+
* Definition for a binary tree node.
21+
* struct TreeNode {
22+
* int val;
23+
* TreeNode *left;
24+
* TreeNode *right;
25+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
26+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
27+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
28+
* };
29+
*/
30+
class Solution {
31+
public:
32+
vector<vector<int>> levelOrder(TreeNode* root) {
33+
vector<vector<int>> out;
34+
queue<TreeNode*> q;
35+
if (!root)return {};
36+
q.push(root);
37+
while (!q.empty()) {
38+
vector<int>tmp;
39+
int size = q.size();
40+
for (int i = 0; i < size; i++) {
41+
tmp.push_back(q.front()->val);
42+
if (q.front()->left != nullptr)
43+
q.push(q.front()->left);
44+
if (q.front()->right != nullptr)
45+
q.push(q.front()->right);
46+
q.pop();
47+
}
48+
out.push_back(tmp);
49+
}
50+
return out;
51+
}
52+
};
53+
// @lc code=end
54+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <vector>
2+
using namespace std;
3+
/*
4+
* @lc app=leetcode id=144 lang=cpp
5+
*
6+
* [144] Binary Tree Preorder Traversal
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
vector<int> preorderTraversal(TreeNode* root) {
24+
vector<int>out;
25+
traverse(root, out);
26+
return out;
27+
}
28+
void traverse(TreeNode* root, vector<int>& out) {
29+
if (root == nullptr)return;
30+
out.push_back(root->val);
31+
traverse(root->left, out);
32+
traverse(root->right, out);
33+
}
34+
};
35+
// @lc code=end
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=145 lang=cpp
3+
*
4+
* [145] Binary Tree Postorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
vector<int> postorderTraversal(TreeNode* root) {
22+
vector<int>out;
23+
traverse(root, out);
24+
return out;
25+
}
26+
void traverse(TreeNode* root, vector<int>& out) {
27+
if (root == nullptr)return;
28+
traverse(root->left, out);
29+
traverse(root->right, out);
30+
out.push_back(root->val);
31+
}
32+
33+
34+
};
35+
// @lc code=end
36+

347.top-k-frequent-elements.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
#include <queue>
4+
5+
using namespace std;
6+
/*
7+
* @lc app=leetcode id=347 lang=cpp
8+
*
9+
* [347] Top K Frequent Elements
10+
*/
11+
12+
// @lc code=start
13+
class Solution {
14+
class compare {
15+
public:
16+
bool operator()(pair<int, int>& a, pair<int, int>& b) {
17+
return a.second < b.second;
18+
}
19+
};
20+
public:
21+
vector<int> topKFrequent(vector<int>& nums, int k) {
22+
unordered_map<int, int>numMap;
23+
vector< int>out;
24+
for (auto i : nums) {
25+
numMap[i]++;
26+
}
27+
priority_queue < pair<int, int>, vector<pair<int, int>>, compare>q;
28+
for (auto i : numMap) {
29+
q.push(i);
30+
}
31+
while (k--) {
32+
out.push_back(q.top().first);
33+
q.pop();
34+
}
35+
return out;
36+
}
37+
};
38+
// @lc code=end
39+
40+
int main() {
41+
Solution s;
42+
vector<int>nums{ 1, 1, 1, 2, 2, 3 };
43+
s.topKFrequent(nums, 2);
44+
return 0;
45+
}

94.binary-tree-inorder-traversal.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=94 lang=cpp
3+
*
4+
* [94] Binary Tree Inorder Traversal
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* struct TreeNode {
11+
* int val;
12+
* TreeNode *left;
13+
* TreeNode *right;
14+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
15+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
16+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
17+
* };
18+
*/
19+
class Solution {
20+
public:
21+
vector<int> inorderTraversal(TreeNode* root) {
22+
vector<int>out;
23+
traverse(root, out);
24+
return out;
25+
}
26+
void traverse(TreeNode* root, vector<int>& out) {
27+
if (root == nullptr)return;
28+
traverse(root->left, out);
29+
out.push_back(root->val);
30+
traverse(root->right, out);
31+
}
32+
33+
34+
};
35+
// @lc code=end
36+

0 commit comments

Comments
 (0)