Skip to content

Commit 6627aff

Browse files
authored
GH-169: Solve leetcode 101 (#182)
1 parent 3f64ba0 commit 6627aff

File tree

21 files changed

+319
-5
lines changed

21 files changed

+319
-5
lines changed

collections/leetcode/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Leetcode
22

3-
Leetcode Solutions
3+
**Leetcode Solutions**
44

55
* 🟢 Easy
66
* 🟡 Medium
77
* 🔴 Hard
88

9+
**Problems**
10+
911
<table>
1012
<tr><td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode1'>1</a></td>
1113
<td>2</td>
@@ -117,7 +119,7 @@ Leetcode Solutions
117119
<td>99</td>
118120
<td>100</td>
119121
<tr>
120-
<td>101</td>
122+
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode101'>101</a></td>
121123
<td>102</td>
122124
<td>103</td>
123125
<td>104</td>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ problems:
1414
- name: 78
1515
languages: cpp
1616
level: medium
17+
- name: 101
18+
languages: cpp
19+
level: easy
1720
- name: 142
1821
languages: cpp
1922
level: medium

problems/leetcode101/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
solution
2+
*.dSYM

problems/leetcode101/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Leetcode Problem
2+
3+
## Usage
4+
5+
Run program with an example
6+
7+
```
8+
bazel run cpp/main:solution < data/1.in
9+
```
10+
11+
Test program
12+
13+
```
14+
# Run all tests
15+
bazel test --test_output=all tests:solution_test
16+
bazel test --test_output=all --cache_test_results=no tests:solution_test
17+
# Run test with pecific test id
18+
bazel test --test_output=all tests:solution_test --test_arg=1
19+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "solution.h"
2+
3+
#include <algorithm>
4+
#include <cmath>
5+
#include <fstream>
6+
#include <iostream>
7+
#include <map>
8+
#include <string>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
TreeNode* parse_tree(string s) {
14+
// parse tree from string with format
15+
// 1 2 3 4 5 6 null 7
16+
string value = "";
17+
vector<TreeNode*> nodes;
18+
TreeNode* node;
19+
for (int i = 0; i < s.size(); i++) {
20+
if (s[i] == ' ') {
21+
if (value == "null") {
22+
node = nullptr;
23+
} else {
24+
node = new TreeNode(stoi(value));
25+
}
26+
value = "";
27+
nodes.push_back(node);
28+
} else {
29+
value += s[i];
30+
}
31+
}
32+
if (value == "null") {
33+
node = nullptr;
34+
} else {
35+
node = new TreeNode(stoi(value));
36+
}
37+
nodes.push_back(node);
38+
int n = nodes.size();
39+
queue<TreeNode*> q;
40+
TreeNode* head = nodes[0];
41+
q.push(head);
42+
int i = 1;
43+
while (!q.empty() && i < n) {
44+
node = q.front();
45+
q.pop();
46+
node->left = nodes[i];
47+
node->right = nodes[i + 1];
48+
q.push(nodes[i]);
49+
q.push(nodes[i + 1]);
50+
i += 2;
51+
}
52+
return head;
53+
}
54+
55+
int main() {
56+
ios::sync_with_stdio(false);
57+
cin.tie(0);
58+
59+
bool is_use_file = false;
60+
61+
filesystem::path filepath =
62+
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
63+
ifstream file(filepath);
64+
65+
if (is_use_file) {
66+
cin.rdbuf(file.rdbuf()); // redirect cin to file
67+
}
68+
69+
// get input
70+
string s;
71+
getline(cin, s);
72+
73+
cout << "s = " << s << endl;
74+
75+
TreeNode* root = parse_tree(s);
76+
77+
// TreeNode* root = new TreeNode(0);
78+
79+
Solution solution;
80+
bool output = solution.isSymmetric(root);
81+
82+
// print output
83+
cout << output << endl;
84+
85+
return 0;
86+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <map>
4+
#include <queue>
5+
#include <set>
6+
#include <stack>
7+
#include <string>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
using namespace std;
12+
13+
struct TreeNode {
14+
int val;
15+
TreeNode *left;
16+
TreeNode *right;
17+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
18+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
19+
TreeNode(int x, TreeNode *left, TreeNode *right)
20+
: val(x), left(left), right(right) {}
21+
};
22+
23+
class Solution {
24+
public:
25+
bool isEquals(vector<TreeNode *> lefts, vector<TreeNode *> rights) {
26+
if (lefts.size() != rights.size()) {
27+
return false;
28+
}
29+
int n = lefts.size();
30+
TreeNode *left, *right;
31+
for (int i = 0; i < lefts.size(); i++) {
32+
left = lefts[i];
33+
right = rights[n - 1 - i];
34+
if (left == nullptr && right == nullptr) {
35+
continue;
36+
} else if (left != nullptr && right != nullptr) {
37+
if (left->val != right->val) {
38+
return false;
39+
}
40+
} else {
41+
return false;
42+
}
43+
}
44+
vector<TreeNode *> left_nexts, right_nexts;
45+
int num_not_nulls = 0;
46+
for (int i = 0; i < lefts.size(); i++) {
47+
left = lefts[i];
48+
right = rights[i];
49+
if (left != nullptr) {
50+
num_not_nulls += 1;
51+
left_nexts.push_back(left->left);
52+
left_nexts.push_back(left->right);
53+
}
54+
if (right != nullptr) {
55+
right_nexts.push_back(right->left);
56+
right_nexts.push_back(right->right);
57+
}
58+
}
59+
if (num_not_nulls > 0) {
60+
return isEquals(left_nexts, right_nexts);
61+
} else {
62+
return true;
63+
}
64+
}
65+
66+
bool isSymmetric(TreeNode *root) {
67+
if (!root) {
68+
return true;
69+
}
70+
return isEquals({root->left}, {root->right});
71+
}
72+
};

problems/leetcode101/data/1.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 2 2 3 4 4 3

0 commit comments

Comments
 (0)