Skip to content

Commit 4a4f6cb

Browse files
committed
feat: add cpp solutions to lcof problems: No. 32,33,34,35,36,39
1 parent dbd354d commit 4a4f6cb

File tree

12 files changed

+288
-0
lines changed

12 files changed

+288
-0
lines changed

lcof/面试题32 - III. 从上到下打印二叉树 III/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ func levelOrder(root *TreeNode) [][]int {
179179
}
180180
```
181181

182+
### **C++**
183+
184+
```cpp
185+
class Solution {
186+
public:
187+
vector<vector<int>> levelOrder(TreeNode* root) {
188+
vector<vector<int>> ans;
189+
if (root == NULL) return ans;
190+
queue<TreeNode*> q;
191+
q.push(root);
192+
bool flag = false;
193+
while (!q.empty()) {
194+
int n = q.size();
195+
vector<int> v;
196+
for (int i = 0; i < n; ++i) {
197+
TreeNode* node = q.front();
198+
q.pop();
199+
v.emplace_back(node->val);
200+
if (node->left) q.push(node->left);
201+
if (node->right) q.push(node->right);
202+
}
203+
if (flag) reverse(v.begin(), v.end());
204+
flag = !flag;
205+
ans.emplace_back(v);
206+
}
207+
return ans;
208+
}
209+
};
210+
```
211+
182212
### **...**
183213
184214
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> levelOrder(TreeNode* root) {
4+
vector<vector<int>> ans;
5+
if (root == NULL) return ans;
6+
queue<TreeNode*> q;
7+
q.push(root);
8+
bool flag = false;
9+
while (!q.empty()) {
10+
int n = q.size();
11+
vector<int> v;
12+
for (int i = 0; i < n; ++i) {
13+
TreeNode* node = q.front();
14+
q.pop();
15+
v.emplace_back(node->val);
16+
if (node->left) q.push(node->left);
17+
if (node->right) q.push(node->right);
18+
}
19+
if (flag) reverse(v.begin(), v.end());
20+
flag = !flag;
21+
ans.emplace_back(v);
22+
}
23+
return ans;
24+
}
25+
};

lcof/面试题33. 二叉搜索树的后序遍历序列/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,35 @@ func helper(postorder []int , left,right int) bool {
149149
}
150150
```
151151

152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
bool verifyPostorder(vector<int>& postorder) {
158+
return verify(postorder, 0, postorder.size() - 1);
159+
}
160+
161+
bool verify(vector<int>& postorder, int left, int right) {
162+
if (left >= right) {
163+
return true;
164+
}
165+
int root = postorder[right], i = left;
166+
while (postorder[i] < root) {
167+
++i;
168+
}
169+
int mid = i;
170+
while (i < right) {
171+
if (postorder[i] < root) {
172+
return false;
173+
}
174+
++i;
175+
}
176+
return verify(postorder, left, mid - 1) && verify(postorder, mid, right - 1);
177+
}
178+
};
179+
```
180+
152181
### **...**
153182

154183
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
bool verifyPostorder(vector<int>& postorder) {
4+
return verify(postorder, 0, postorder.size() - 1);
5+
}
6+
7+
bool verify(vector<int>& postorder, int left, int right) {
8+
if (left >= right) {
9+
return true;
10+
}
11+
int root = postorder[right], i = left;
12+
while (postorder[i] < root) {
13+
++i;
14+
}
15+
int mid = i;
16+
while (i < right) {
17+
if (postorder[i] < root) {
18+
return false;
19+
}
20+
++i;
21+
}
22+
return verify(postorder, left, mid - 1) && verify(postorder, mid, right - 1);
23+
}
24+
};

lcof/面试题34. 二叉树中和为某一值的路径/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,36 @@ func helper(node *TreeNode, target int, ans []int) {
176176
}
177177
```
178178

179+
### **C++**
180+
181+
```cpp
182+
class Solution {
183+
public:
184+
vector<vector<int>> pathSum(TreeNode* root, int target) {
185+
vector<vector<int>> ans;
186+
vector<int> path;
187+
dfs(root, ans, path, target);
188+
return ans;
189+
}
190+
191+
void dfs(TreeNode* root, vector<vector<int>>& ans, vector<int>& path, int target) {
192+
if (root == NULL) {
193+
return;
194+
}
195+
target -= root->val;
196+
path.push_back(root->val);
197+
if (root->left == NULL && root->right == NULL) {
198+
if (target == 0) {
199+
ans.push_back(vector<int>(path));
200+
}
201+
}
202+
dfs(root->left, ans, path, target);
203+
dfs(root->right, ans, path, target);
204+
path.pop_back();
205+
}
206+
};
207+
```
208+
179209
### **...**
180210

181211
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> pathSum(TreeNode* root, int target) {
4+
vector<vector<int>> ans;
5+
vector<int> path;
6+
dfs(root, ans, path, target);
7+
return ans;
8+
}
9+
10+
void dfs(TreeNode* root, vector<vector<int>>& ans, vector<int>& path, int target) {
11+
if (root == NULL) {
12+
return;
13+
}
14+
target -= root->val;
15+
path.push_back(root->val);
16+
if (root->left == NULL && root->right == NULL) {
17+
if (target == 0) {
18+
ans.push_back(vector<int>(path));
19+
}
20+
}
21+
dfs(root->left, ans, path, target);
22+
dfs(root->right, ans, path, target);
23+
path.pop_back();
24+
}
25+
};

lcof/面试题35. 复杂链表的复制/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,29 @@ func copyRandomList(head *Node) *Node {
209209
}
210210
```
211211

212+
### **C++**
213+
214+
```cpp
215+
class Solution {
216+
public:
217+
Node* copyRandomList(Node* head) {
218+
unordered_map<Node*, Node*> m;
219+
Node* p = head;
220+
while (p) {
221+
m[p] = new Node(p->val);
222+
p = p->next;
223+
}
224+
p = head;
225+
while (p) {
226+
m[p]->next = m[p->next];
227+
m[p]->random = m[p->random];
228+
p = p->next;
229+
}
230+
return m[head];
231+
}
232+
};
233+
```
234+
212235
### **...**
213236
214237
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
Node* copyRandomList(Node* head) {
4+
unordered_map<Node*, Node*> m;
5+
Node* p = head;
6+
while (p) {
7+
m[p] = new Node(p->val);
8+
p = p->next;
9+
}
10+
p = head;
11+
while (p) {
12+
m[p]->next = m[p->next];
13+
m[p]->random = m[p->random];
14+
p = p->next;
15+
}
16+
return m[head];
17+
}
18+
};

lcof/面试题36. 二叉搜索树与双向链表/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,37 @@ var treeToDoublyList = function (root) {
149149
};
150150
```
151151

152+
### **C++**
153+
154+
```cpp
155+
class Solution {
156+
public:
157+
Node* treeToDoublyList(Node* root) {
158+
if (root == NULL) return NULL;
159+
inorder(root);
160+
head->left = pre;
161+
pre->right = head;
162+
return head;
163+
}
164+
165+
private:
166+
Node *pre, *head;
167+
168+
void inorder(Node* cur) {
169+
if (cur) {
170+
inorder(cur->left);
171+
if (pre)
172+
pre->right = cur;
173+
else
174+
head = cur;
175+
cur->left = pre;
176+
pre = cur;
177+
inorder(cur->right);
178+
}
179+
}
180+
};
181+
```
182+
152183
### **...**
153184

154185
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
Node* treeToDoublyList(Node* root) {
4+
if (root == NULL) return NULL;
5+
inorder(root);
6+
head->left = pre;
7+
pre->right = head;
8+
return head;
9+
}
10+
11+
private:
12+
Node *pre, *head;
13+
14+
void inorder(Node* cur) {
15+
if (cur) {
16+
inorder(cur->left);
17+
if (pre)
18+
pre->right = cur;
19+
else
20+
head = cur;
21+
cur->left = pre;
22+
pre = cur;
23+
inorder(cur->right);
24+
}
25+
}
26+
};

lcof/面试题39. 数组中出现次数超过一半的数字/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,22 @@ var majorityElement = function (nums) {
8181
};
8282
```
8383

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int majorityElement(vector<int>& nums) {
90+
int votes = 0, x = 0;
91+
for (int num : nums) {
92+
if (votes == 0) x = num;
93+
votes += x == num ? 1 : -1;
94+
}
95+
return x;
96+
}
97+
};
98+
```
99+
84100
### **...**
85101
86102
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
int majorityElement(vector<int>& nums) {
4+
int votes = 0, x = 0;
5+
for (int num : nums) {
6+
if (votes == 0) x = num;
7+
votes += x == num ? 1 : -1;
8+
}
9+
return x;
10+
}
11+
};

0 commit comments

Comments
 (0)