Skip to content

Commit 62e3355

Browse files
committed
feat: add cpp solution to lc problem: No.0144. Binary Tree Preorder Traversal
1 parent 5f31081 commit 62e3355

File tree

12 files changed

+369
-52
lines changed

12 files changed

+369
-52
lines changed

Diff for: solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md

+43-9
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,17 @@ class Solution:
112112
# self.right = right
113113
class Solution:
114114
def preorderTraversal(self, root: TreeNode) -> List[int]:
115+
if root is None:
116+
return []
115117
res = []
116-
if root:
117-
s = [root]
118-
while s:
119-
node = s.pop()
120-
res.append(node.val)
121-
if node.right:
122-
s.append(node.right)
123-
if node.left:
124-
s.append(node.left)
118+
s = [root]
119+
while s:
120+
node = s.pop()
121+
res.append(node.val)
122+
if node.right:
123+
s.append(node.right)
124+
if node.left:
125+
s.append(node.left)
125126
return res
126127
```
127128

@@ -208,6 +209,39 @@ class Solution {
208209
}
209210
```
210211

212+
### **C++**
213+
214+
```cpp
215+
/**
216+
* Definition for a binary tree node.
217+
* struct TreeNode {
218+
* int val;
219+
* TreeNode *left;
220+
* TreeNode *right;
221+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
222+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
223+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
224+
* };
225+
*/
226+
class Solution {
227+
public:
228+
vector<int> preorderTraversal(TreeNode* root) {
229+
vector<int> res;
230+
if (root == nullptr) return res;
231+
stack<TreeNode*> s;
232+
s.push(root);
233+
while (!s.empty()) {
234+
TreeNode *node = s.top();
235+
s.pop();
236+
res.push_back(node->val);
237+
if (node->right) s.push(node->right);
238+
if (node->left) s.push(node->left);
239+
}
240+
return res;
241+
}
242+
};
243+
```
244+
211245
### **...**
212246
213247
```

Diff for: solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md

+43-9
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ Non-recursive:
9292
# self.right = right
9393
class Solution:
9494
def preorderTraversal(self, root: TreeNode) -> List[int]:
95+
if root is None:
96+
return []
9597
res = []
96-
if root:
97-
s = [root]
98-
while s:
99-
node = s.pop()
100-
res.append(node.val)
101-
if node.right:
102-
s.append(node.right)
103-
if node.left:
104-
s.append(node.left)
98+
s = [root]
99+
while s:
100+
node = s.pop()
101+
res.append(node.val)
102+
if node.right:
103+
s.append(node.right)
104+
if node.left:
105+
s.append(node.left)
105106
return res
106107
```
107108

@@ -186,6 +187,39 @@ class Solution {
186187
}
187188
```
188189

190+
### **C++**
191+
192+
```cpp
193+
/**
194+
* Definition for a binary tree node.
195+
* struct TreeNode {
196+
* int val;
197+
* TreeNode *left;
198+
* TreeNode *right;
199+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
200+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
201+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
202+
* };
203+
*/
204+
class Solution {
205+
public:
206+
vector<int> preorderTraversal(TreeNode* root) {
207+
vector<int> res;
208+
if (root == nullptr) return res;
209+
stack<TreeNode*> s;
210+
s.push(root);
211+
while (!s.empty()) {
212+
TreeNode *node = s.top();
213+
s.pop();
214+
res.push_back(node->val);
215+
if (node->right) s.push(node->right);
216+
if (node->left) s.push(node->left);
217+
}
218+
return res;
219+
}
220+
};
221+
```
222+
189223
### **...**
190224
191225
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> preorderTraversal(TreeNode* root) {
15+
vector<int> res;
16+
if (root == nullptr) return res;
17+
stack<TreeNode*> s;
18+
s.push(root);
19+
while (!s.empty()) {
20+
TreeNode *node = s.top();
21+
s.pop();
22+
res.push_back(node->val);
23+
if (node->right) s.push(node->right);
24+
if (node->left) s.push(node->left);
25+
}
26+
return res;
27+
}
28+
};

Diff for: solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
* }
1515
*/
1616
class Solution {
17-
18-
private List<Integer> res;
19-
2017
public List<Integer> preorderTraversal(TreeNode root) {
21-
res = new ArrayList<>();
22-
preorder(root);
23-
return res;
24-
}
25-
26-
private void preorder(TreeNode root) {
27-
if (root != null) {
28-
res.add(root.val);
29-
preorder(root.left);
30-
preorder(root.right);
18+
if (root == null) {
19+
return Collections.emptyList();
3120
}
21+
List<Integer> res = new ArrayList<>();
22+
Deque<TreeNode> s = new ArrayDeque<>();
23+
s.push(root);
24+
while (!s.isEmpty()) {
25+
TreeNode node = s.pop();
26+
res.add(node.val);
27+
if (node.right != null) {
28+
s.push(node.right);
29+
}
30+
if (node.left != null) {
31+
s.push(node.left);
32+
}
33+
}
34+
return res;
3235
}
3336
}

Diff for: solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
# self.right = right
77
class Solution:
88
def preorderTraversal(self, root: TreeNode) -> List[int]:
9-
def preorder(root):
10-
if root:
11-
res.append(root.val)
12-
preorder(root.left)
13-
preorder(root.right)
9+
if root is None:
10+
return []
1411
res = []
15-
preorder(root)
12+
s = [root]
13+
while s:
14+
node = s.pop()
15+
res.append(node.val)
16+
if node.right:
17+
s.append(node.right)
18+
if node.left:
19+
s.append(node.left)
1620
return res

Diff for: solution/1900-1999/1934.Confirmation Rate/README.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# [1934. Confirmation Rate](https://leetcode-cn.com/problems/confirmation-rate)
2+
3+
[English Version](/solution/1900-1999/1934.Confirmation%20Rate/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Signups</code></p>
10+
11+
<pre>
12+
+----------------+----------+
13+
| Column Name | Type |
14+
+----------------+----------+
15+
| user_id | int |
16+
| time_stamp | datetime |
17+
+----------------+----------+
18+
user_id is the primary key for this table.
19+
Each row contains information about the signup time for the user with ID user_id.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Table: <code>Confirmations</code></p>
25+
26+
<pre>
27+
+----------------+----------+
28+
| Column Name | Type |
29+
+----------------+----------+
30+
| user_id | int |
31+
| time_stamp | datetime |
32+
| action | ENUM |
33+
+----------------+----------+
34+
(user_id, time_stamp) is the primary key for this table.
35+
user_id is a foreign key with a reference to the Signups table.
36+
action is an ENUM of the type (&#39;confirmed&#39;, &#39;timeout&#39;)
37+
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed (&#39;confirmed&#39;) or expired without confirming (&#39;timeout&#39;).</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p>The <strong>confirmation rate</strong> of a user is the number of <code>&#39;confirmed&#39;</code> messages divided by the total number of requested confirmation messages. The confirmation rate of a user that did not request any confirmation messages is <code>0</code>.</p>
42+
43+
<p>Write an SQL query to find the <strong>confirmation rate</strong> of each user.</p>
44+
45+
<p>Return the result table <strong>in any order</strong>.</p>
46+
47+
<p>The query result format is in the following example:</p>
48+
49+
<p>&nbsp;</p>
50+
51+
<pre>
52+
Signups table:
53+
+---------+---------------------+
54+
| user_id | time_stamp |
55+
+---------+---------------------+
56+
| 3 | 2020-03-21 10:16:13 |
57+
| 7 | 2020-01-04 13:57:59 |
58+
| 2 | 2020-07-29 23:09:44 |
59+
| 6 | 2020-12-09 10:39:37 |
60+
+---------+---------------------+
61+
62+
Confirmations table:
63+
+---------+---------------------+-----------+
64+
| user_id | time_stamp | action |
65+
+---------+---------------------+-----------+
66+
| 3 | 2021-01-06 03:30:46 | timeout |
67+
| 3 | 2021-07-14 14:00:00 | timeout |
68+
| 7 | 2021-06-12 11:57:29 | confirmed |
69+
| 7 | 2021-06-13 12:58:28 | confirmed |
70+
| 7 | 2021-06-14 13:59:27 | confirmed |
71+
| 2 | 2021-01-22 00:00:00 | confirmed |
72+
| 2 | 2021-02-28 23:59:59 | timeout |
73+
+---------+---------------------+-----------+
74+
75+
Result table
76+
+---------+-------------------+
77+
| user_id | confirmation_rate |
78+
+---------+-------------------+
79+
| 6 | 0.00 |
80+
| 3 | 0.00 |
81+
| 7 | 1.00 |
82+
| 2 | 0.50 |
83+
+---------+-------------------+
84+
85+
User 6 did not request any confirmation messages. The confirmation rate is 0.
86+
User 3 made 2 requests and both timed out. The confirmation rate is 0.
87+
User 7 made 3 requests and all were confirmed. The confirmation rate is 1.
88+
User 2 made 2 requests where one was confirmed and the other timed out. The confirmation rate is 1 / 2 = 0.5.
89+
</pre>
90+
91+
92+
## 解法
93+
94+
<!-- 这里可写通用的实现逻辑 -->
95+
96+
<!-- tabs:start -->
97+
98+
### **SQL**
99+
100+
<!-- 这里可写当前语言的特殊实现逻辑 -->
101+
102+
```sql
103+
104+
```
105+
106+
<!-- tabs:end -->

0 commit comments

Comments
 (0)