File tree 4 files changed +156
-0
lines changed
4 files changed +156
-0
lines changed Original file line number Diff line number Diff line change @@ -148,6 +148,34 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
148
148
}
149
149
```
150
150
151
+ ### ** C++**
152
+
153
+ ``` cpp
154
+ class Solution {
155
+ public:
156
+ ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
157
+ if (nullptr == l1 && nullptr == l2) {
158
+ return nullptr; // 两个都为空,则直接返回
159
+ }
160
+
161
+ if (nullptr == l1 || nullptr == l2) {
162
+ return l1 == nullptr ? l2 : l1; // 有且仅有一个为空,则返回非空节点
163
+ }
164
+
165
+ ListNode* node = nullptr ;
166
+ if (l1->val > l2->val) {
167
+ node = l2;
168
+ node->next = mergeTwoLists(l1, l2->next);
169
+ } else {
170
+ node = l1;
171
+ node->next = mergeTwoLists(l1->next, l2);
172
+ }
173
+
174
+ return node;
175
+ }
176
+ };
177
+ ```
178
+
151
179
### ** ...**
152
180
153
181
```
Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode(int x) : val(x), next(NULL) {}
7
+ * };
8
+ */
9
+
10
+ class Solution {
11
+ public:
12
+ ListNode* mergeTwoLists (ListNode* l1, ListNode* l2) {
13
+ if (nullptr == l1 && nullptr == l2) {
14
+ return nullptr ;
15
+ }
16
+
17
+ if (nullptr == l1 || nullptr == l2) {
18
+ return l1 == nullptr ? l2 : l1;
19
+ }
20
+
21
+ ListNode* node = nullptr ;
22
+ if (l1->val > l2->val ) {
23
+ node = l2;
24
+ node->next = mergeTwoLists (l1, l2->next );
25
+ } else {
26
+ node = l1;
27
+ node->next = mergeTwoLists (l1->next , l2);
28
+ }
29
+
30
+ return node;
31
+ }
32
+ };
Original file line number Diff line number Diff line change @@ -162,6 +162,53 @@ func helper(a *TreeNode, b *TreeNode) bool {
162
162
}
163
163
```
164
164
165
+
166
+ ### ** C++**
167
+
168
+ ``` cpp
169
+ class Solution {
170
+ public:
171
+ bool isSubTree(TreeNode* a, TreeNode* b) {
172
+ if (nullptr == b) {
173
+ // 如果小树走到头,则表示ok了
174
+ return true;
175
+ }
176
+
177
+ if (nullptr == a) {
178
+ // 如果大树走到头,小树却没走到头,说明不对了
179
+ return false;
180
+ }
181
+
182
+ if (a->val != b->val) {
183
+ return false;
184
+ }
185
+
186
+ return isSubTree(a->left, b->left) && isSubTree(a->right, b->right);
187
+ }
188
+
189
+ bool isSubStructure(TreeNode* a, TreeNode* b) {
190
+ bool ret = false;
191
+ if (nullptr != a && nullptr != b) {
192
+ // 题目约定,空树不属于任何一个数的子树
193
+ if (a->val == b->val) {
194
+ // 如果值相等,才进入判定
195
+ ret = isSubTree(a, b);
196
+ }
197
+
198
+ if (false == ret) {
199
+ ret = isSubStructure(a->left, b);
200
+ }
201
+
202
+ if (false == ret) {
203
+ ret = isSubStructure(a->right, b);
204
+ }
205
+ }
206
+
207
+ return ret;
208
+ }
209
+ };
210
+ ```
211
+
165
212
### ** ...**
166
213
167
214
```
Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+ class Solution {
11
+ public:
12
+ bool isSubTree (TreeNode* a, TreeNode* b) {
13
+ if (nullptr == b) {
14
+ // 如果小树走到头,则表示ok了
15
+ return true ;
16
+ }
17
+
18
+ if (nullptr == a) {
19
+ // 如果大树走到头,小树却没走到头,说明不对了
20
+ return false ;
21
+ }
22
+
23
+ if (a->val != b->val ) {
24
+ return false ;
25
+ }
26
+
27
+ return isSubTree (a->left , b->left ) && isSubTree (a->right , b->right );
28
+ }
29
+
30
+ bool isSubStructure (TreeNode* a, TreeNode* b) {
31
+ bool ret = false ;
32
+ if (nullptr != a && nullptr != b) {
33
+ if (a->val == b->val ) {
34
+ // 如果值相等,才进入判定
35
+ ret = isSubTree (a, b);
36
+ }
37
+
38
+ if (false == ret) {
39
+ ret = isSubStructure (a->left , b);
40
+ }
41
+
42
+ if (false == ret) {
43
+ ret = isSubStructure (a->right , b);
44
+ }
45
+ }
46
+
47
+ return ret;
48
+ }
49
+ };
You can’t perform that action at this time.
0 commit comments