Skip to content

Commit 49dd49f

Browse files
committed
namespace insert_into_a_binary_search_tree
1 parent 5820539 commit 49dd49f

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
// +build ignore
22

33
#pragma once
4-
// #ifndef _insert_into_a_binary_search_tree_SOLUTION_
5-
// #define _insert_into_a_binary_search_tree_SOLUTION_
64

7-
// #include "TreeNode.hpp"
85
#include <stdio.h>
96
import leetcode_treenode_cpp.TreeNode;
107
using namespace leetcode_treenode_cpp;
11-
class Solution {
12-
public:
13-
TreeNode* insertIntoBST(TreeNode* root, int val)
8+
9+
namespace insert_into_a_binary_search_tree
10+
{
11+
class Solution
1412
{
15-
if (root == NULL) {
16-
TreeNode* node = new TreeNode(val);
17-
return node;
13+
public:
14+
TreeNode *insertIntoBST(TreeNode *root, int val)
15+
{
16+
if (root == NULL)
17+
{
18+
TreeNode *node = new TreeNode(val);
19+
return node;
20+
}
21+
if (root->val > val)
22+
root->left = insertIntoBST(root->left, val);
23+
if (root->val < val)
24+
root->right = insertIntoBST(root->right, val);
25+
return root;
1826
}
19-
if (root->val > val)
20-
root->left = insertIntoBST(root->left, val);
21-
if (root->val < val)
22-
root->right = insertIntoBST(root->right, val);
23-
return root;
24-
}
25-
};
26-
// #endif //
27+
};
28+
29+
}

insert-into-a-binary-search-tree/test.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import leetcode_treenode_cpp.freeTreeNode;
3030
import leetcode_treenode_cpp.serializeTreeNode;
3131
import leetcode_treenode_cpp.LeetCodeTreeNodeToString;
3232
import leetcode_treenode_cpp.parseLeetCodeBinaryTree;
33+
using namespace insert_into_a_binary_search_tree;
3334
void println(int s)
3435
{
3536
cout << s << endl;
@@ -53,18 +54,19 @@ void test1()
5354
{
5455
println("insert-into-a-binary-search-tree");
5556
println("test1 start");
56-
TreeNode* none = NULL;
57+
TreeNode *none = NULL;
5758

5859
auto result2 = Solution().insertIntoBST(none, 111);
5960

6061
println(serializeTreeNode(none));
6162
assertEquals(serializeTreeNode(none), "null");
6263
println(serializeTreeNode(result2));
6364
assertEquals(serializeTreeNode(result2),
64-
"TreeNode{val:111,left:null,right:null}");
65+
"TreeNode{val:111,left:null,right:null}");
6566

66-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { none, result2 };
67-
for (auto node : nodes) {
67+
auto nodes = unordered_set<TreeNode *, HashTreeNode, EqualTreeNode>{none, result2};
68+
for (auto node : nodes)
69+
{
6870
printTreeNode(node);
6971
freeTreeNode(node);
7072
}
@@ -77,27 +79,30 @@ void test2()
7779
auto tree = new TreeNode(99);
7880
println(serializeTreeNode(tree));
7981
assertEquals(serializeTreeNode(tree),
80-
"TreeNode{val:99,left:null,right:null}");
82+
"TreeNode{val:99,left:null,right:null}");
8183
auto result = Solution().insertIntoBST(tree, 111);
8284

8385
println(serializeTreeNode(result));
8486
assertEquals(serializeTreeNode(result),
85-
"TreeNode{val:99,left:null,right:TreeNode{val:111,left:null,"
86-
"right:null}}");
87+
"TreeNode{val:99,left:null,right:TreeNode{val:111,left:null,"
88+
"right:null}}");
8789

88-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { tree, result };
89-
for (auto node : nodes) {
90+
auto nodes = unordered_set<TreeNode *, HashTreeNode, EqualTreeNode>{tree, result};
91+
for (auto node : nodes)
92+
{
9093
printTreeNode(node);
9194
freeTreeNode(node);
9295
}
9396
println("test2 end");
9497
}
95-
struct ExampleType {
98+
struct ExampleType
99+
{
96100
string root;
97101
int val;
98102
string output;
99103
};
100-
class StringTest : public CppUnit::TestFixture {
104+
class StringTest : public CppUnit::TestFixture
105+
{
101106
CPPUNIT_TEST_SUITE(StringTest);
102107
CPPUNIT_TEST(testSwap);
103108
CPPUNIT_TEST(testFind);
@@ -108,34 +113,36 @@ class StringTest : public CppUnit::TestFixture {
108113
CPPUNIT_TEST_SUITE_END();
109114

110115
public:
111-
void setUp() { }
116+
void setUp() {}
112117
void test4()
113118
{
114119

115-
auto examples = vector<ExampleType> { { "[4,2,7,1,3]", 5, "[4,2,7,1,3,5]" },
120+
auto examples = vector<ExampleType>{{"[4,2,7,1,3]", 5, "[4,2,7,1,3,5]"},
116121

117-
{ "[40,20,60,10,30,50,70]", 25, "[40,20,60,10,30,50,70,null,null,25]" },
118-
{ "[4,2,7,1,3,null,null,null,null,null,null]", 5, "[4,2,7,1,3,5]" } };
122+
{"[40,20,60,10,30,50,70]", 25, "[40,20,60,10,30,50,70,null,null,25]"},
123+
{"[4,2,7,1,3,null,null,null,null,null,null]", 5, "[4,2,7,1,3,5]"}};
119124

120-
for (auto& example : examples) {
121-
TreeNode* root = nullptr;
125+
for (auto &example : examples)
126+
{
127+
TreeNode *root = nullptr;
122128
int status = parseLeetCodeBinaryTree(example.root, &root);
123129
CPPUNIT_ASSERT_EQUAL(0, status);
124130
auto output = Solution().insertIntoBST(root, example.val);
125131

126132
CPPUNIT_ASSERT_EQUAL(LeetCodeTreeNodeToString(output),
127-
example.output);
133+
example.output);
128134
println(example.root);
129135
println(example.val);
130136
println(example.output);
131-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { root, output };
132-
for (auto node : nodes) {
137+
auto nodes = unordered_set<TreeNode *, HashTreeNode, EqualTreeNode>{root, output};
138+
for (auto node : nodes)
139+
{
133140
printTreeNode(node);
134141
freeTreeNode(node);
135142
}
136143
}
137144
}
138-
void tearDown() { }
145+
void tearDown() {}
139146

140147
void testSwap() { test1(); }
141148

@@ -146,7 +153,7 @@ class StringTest : public CppUnit::TestFixture {
146153
println("test3 start");
147154
auto rawString = string("[4,2,7,1,3]");
148155
auto val = 5;
149-
TreeNode* root = nullptr;
156+
TreeNode *root = nullptr;
150157
int status = parseLeetCodeBinaryTree(rawString, &root);
151158
CPPUNIT_ASSERT_EQUAL(0, status);
152159

@@ -157,7 +164,7 @@ class StringTest : public CppUnit::TestFixture {
157164
println(serializeTreeNode(root));
158165
auto serialized = string("TreeNode{val:4,left:TreeNode{val:2,left:TreeNode{val:1,left:null,right:null},right:TreeNode{val:3,left:null,right:null}},right:TreeNode{val:7,left:null,right:null}}");
159166
CPPUNIT_ASSERT_EQUAL(serializeTreeNode(root),
160-
serialized);
167+
serialized);
161168
freeTreeNode(root);
162169
println("test3 end");
163170
}
@@ -166,7 +173,7 @@ class StringTest : public CppUnit::TestFixture {
166173
println("test5 start");
167174
auto rawString = string("[-4,-2,-7,-1,-3]");
168175
auto val = 5;
169-
TreeNode* root = nullptr;
176+
TreeNode *root = nullptr;
170177
int status = parseLeetCodeBinaryTree(rawString, &root);
171178
CPPUNIT_ASSERT_EQUAL(0, status);
172179

@@ -177,15 +184,15 @@ class StringTest : public CppUnit::TestFixture {
177184
println(serializeTreeNode(root));
178185
auto serialized = string("TreeNode{val:-4,left:TreeNode{val:-2,left:TreeNode{val:-1,left:null,right:null},right:TreeNode{val:-3,left:null,right:null}},right:TreeNode{val:-7,left:null,right:null}}");
179186
CPPUNIT_ASSERT_EQUAL(serializeTreeNode(root),
180-
serialized);
187+
serialized);
181188
freeTreeNode(root);
182189
println("test5 end");
183190
}
184191
};
185192

186193
CPPUNIT_TEST_SUITE_REGISTRATION(StringTest);
187194

188-
int main(int argc, char* argv[])
195+
int main(int argc, char *argv[])
189196
{
190197
CppUnit::TestResult r;
191198
CppUnit::TestResultCollector rc;

0 commit comments

Comments
 (0)