Skip to content

Commit bad87a2

Browse files
authored
Gh-169: Solve leetcode 106 (#225)
1 parent d51274e commit bad87a2

File tree

14 files changed

+274
-1
lines changed

14 files changed

+274
-1
lines changed

collections/leetcode/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
<td>103</td>
125125
<td>104</td>
126126
<td>105</td>
127-
<td>106</td>
127+
<td>🟡&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode106'>106</a></td>
128128
<td>107</td>
129129
<td>108</td>
130130
<td>109</td>

collections/leetcode/data.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ problems:
2020
- name: 101
2121
languages: cpp
2222
level: easy
23+
- name: 106
24+
languages: cpp
25+
level: medium
2326
- name: 125
2427
languages: cpp
2528
level: easy

problems/leetcode106/.gitignore

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

problems/leetcode106/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+
```

problems/leetcode106/cpp/main/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
3+
cc_binary(
4+
name = "solution",
5+
srcs = ["solution.cpp", "solution.h"],
6+
visibility = ["//visibility:public"],
7+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "solution.h"
2+
3+
#include <algorithm>
4+
#include <cmath>
5+
#include <iostream>
6+
#include <map>
7+
#include <string>
8+
#include <vector>
9+
#include <fstream>
10+
11+
using namespace std;
12+
13+
int main() {
14+
ios::sync_with_stdio(false);
15+
cin.tie(0);
16+
17+
bool is_use_file = false;
18+
19+
filesystem::path filepath =
20+
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
21+
ifstream file(filepath);
22+
23+
if (is_use_file) {
24+
cin.rdbuf(file.rdbuf()); // redirect cin to file
25+
}
26+
27+
// get input
28+
int x;
29+
cin >> x;
30+
31+
vector<int> inorder;
32+
for(int i=0; i<x; i++){
33+
cin >> inorder[x];
34+
}
35+
36+
vector<int> postorder;
37+
for(int i=0; i<x; i++){
38+
cin >> postorder[x];
39+
}
40+
41+
Solution solution;
42+
TreeNode* tree = solution.buildTree(inorder, postorder);
43+
44+
// print output
45+
cout << tree->val << endl;
46+
47+
return 0;
48+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <map>
4+
#include <set>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <vector>
8+
#include <stack>
9+
#include <queue>
10+
11+
using namespace std;
12+
13+
// Definition for a binary tree node.
14+
struct TreeNode {
15+
int val;
16+
TreeNode *left;
17+
TreeNode *right;
18+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
19+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
20+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
21+
};
22+
23+
class Solution {
24+
public:
25+
TreeNode* solve(vector<int>&inorder, vector<int>& postorder, int i0, int i1, int p0, int p1){
26+
if(p0 == p1){
27+
return new TreeNode(postorder[p0]);
28+
}
29+
int val = postorder[p1];
30+
TreeNode* root = new TreeNode(val);
31+
int index;
32+
// search val in inorder
33+
for(int i=i0; i<=i1; i++){
34+
if(inorder[i] == val){
35+
index = i;
36+
break;
37+
}
38+
}
39+
TreeNode* left;
40+
TreeNode* right;
41+
if(index == i0){
42+
left = nullptr;
43+
} else {
44+
left = solve(
45+
inorder, postorder,
46+
i0, index-1,
47+
p0, p0 + index - i0 -1
48+
);
49+
}
50+
root->left = left;
51+
52+
if(index == i1){
53+
right = nullptr;
54+
} else {
55+
int size = i1 - (index + 1);
56+
right = solve(
57+
inorder, postorder,
58+
index+1, i1,
59+
p1-1-size, p1-1
60+
);
61+
}
62+
root->right = right;
63+
return root;
64+
}
65+
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
66+
return solve(
67+
inorder, postorder,
68+
0, inorder.size()-1,
69+
0, postorder.size()-1);
70+
}
71+
};

problems/leetcode106/cpp/tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")
2+
3+
cc_test(
4+
name = "solution_test",
5+
srcs = ["solution_test.cpp"],
6+
deps = [
7+
"//problems/codeforcesAA/src/main:solution",
8+
"@com_google_googletest//:gtest_main",
9+
],
10+
data = ["data"]
11+
)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <cstdlib>
2+
#include <filesystem>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <string_view>
7+
#include <vector>
8+
9+
#include "gtest/gtest.h"
10+
11+
namespace fs = std::filesystem;
12+
13+
using namespace std;
14+
15+
std::string TEST_ID = "";
16+
17+
vector<string> split(string s, string delimiter) {
18+
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
19+
string token;
20+
vector<string> res;
21+
22+
while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
23+
token = s.substr(pos_start, pos_end - pos_start);
24+
pos_start = pos_end + delim_len;
25+
res.push_back(token);
26+
}
27+
28+
res.push_back(s.substr(pos_start));
29+
return res;
30+
}
31+
32+
std::string GetHello(std::string_view in) {
33+
if (in.size() == 0) {
34+
return std::string("hello, word");
35+
} else {
36+
return std::string("Hello, ") + in.data();
37+
}
38+
}
39+
40+
string ReadFile(const std::string &filename) {
41+
std::ifstream f(filename);
42+
string s((std::istreambuf_iterator<char>(f)),
43+
std::istreambuf_iterator<char>());
44+
return s;
45+
}
46+
47+
vector<string> RunProgram(string fileid) {
48+
vector<string> v;
49+
string program = "problems/codeforcesAA/src/main/solution";
50+
string test_folder = "problems/codeforcesAA/tests/data/";
51+
string command = program + " < " + test_folder + fileid + ".in > output.txt";
52+
std::system(command.c_str());
53+
string actual = ReadFile("output.txt");
54+
string expectedOutputFile = test_folder + fileid + ".out";
55+
string expected = ReadFile(expectedOutputFile);
56+
v.push_back(actual);
57+
v.push_back(expected);
58+
return v;
59+
}
60+
61+
TEST(RunTest, AllTestCases) {
62+
if (TEST_ID != "") {
63+
std::cout << "TEST_ID = " << TEST_ID << std::endl;
64+
} else {
65+
std::cout << "RUN ALL TESTS" << std::endl;
66+
}
67+
68+
std::string test_data_folder = "problems/codeforcesAA/tests/data";
69+
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
70+
string filename = split(entry.path(), "/").back();
71+
vector<string> v = split(filename, ".");
72+
string fileid = v[0];
73+
string extension = v[1];
74+
if (extension != "in"){
75+
continue;
76+
}
77+
if (TEST_ID != ""){
78+
if (fileid != TEST_ID){
79+
continue;
80+
}
81+
}
82+
// Run Test Case
83+
vector<string> output = RunProgram(fileid);
84+
string actual = output[0];
85+
string expected = output[1];
86+
string message = "❌ FAIL CASE: " + fileid;
87+
ASSERT_EQ(actual, expected) << message;
88+
}
89+
}
90+
91+
int main(int argc, char **argv) {
92+
::testing::InitGoogleTest(&argc, argv);
93+
if (argc == 2) {
94+
TEST_ID = argv[1];
95+
}
96+
return RUN_ALL_TESTS();
97+
}

problems/leetcode106/data/1.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5
2+
9 3 15 20 7
3+
9 15 7 20 3

problems/leetcode106/data/1.out

Whitespace-only changes.

problems/leetcode106/data/2.in

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

problems/leetcode106/data/2.out

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Leetcode 78. Subsets",
3+
"link": "https://leetcode.com/problems/subsets/",
4+
"tags": [
5+
"Array",
6+
"Backtracking",
7+
"Bit Manipulation"
8+
]
9+
}

0 commit comments

Comments
 (0)