Skip to content

Commit 21b65cc

Browse files
committed
PostOrderTraversal
1 parent 42c7460 commit 21b65cc

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module;
2+
#include <functional>
3+
export module insert_into_a_binary_search_tree.PostOrderTraversal;
4+
5+
import leetcode_treenode_cpp.TreeNode;
6+
using leetcode_treenode_cpp::TreeNode;
7+
namespace insert_into_a_binary_search_tree {
8+
using std::function;
9+
10+
export void PostOrderTraversal(TreeNode* root, function<void(TreeNode*)> callback)
11+
{
12+
13+
if (root == nullptr) {
14+
15+
return;
16+
}
17+
18+
PostOrderTraversal(root->left, callback);
19+
PostOrderTraversal(root->right, callback);
20+
21+
callback(root);
22+
}
23+
24+
}

insert-into-a-binary-search-tree/insert-into-a-binary-search-tree.vcxproj

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,14 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\bfsTravelsal.ixx">
23-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
24-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
25-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
26-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
27-
</ClCompile>
28-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\freeTreeNode.ixx">
29-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
30-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
31-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
32-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
33-
</ClCompile>
34-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\parseLeetCodeBinaryTree.ixx">
35-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
36-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
37-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
38-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
39-
</ClCompile>
40-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\serializeTreeNode.ixx">
41-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
42-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
43-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
44-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
45-
</ClCompile>
46-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\traversalTreeNode.ixx">
47-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
48-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
49-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
50-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
51-
</ClCompile>
52-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\TreeNode.ixx">
53-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
54-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
55-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
56-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
57-
</ClCompile>
22+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\bfsTravelsal.ixx" />
23+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\freeTreeNode.ixx" />
24+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\LeetCodeTreeNodeToString.ixx" />
25+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\parseLeetCodeBinaryTree.ixx" />
26+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\serializeTreeNode.ixx" />
27+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\traversalTreeNode.ixx" />
28+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\TreeNode.ixx" />
29+
<ClCompile Include="PostOrderTraversal.ixx" />
5830
<ClCompile Include="test.cpp">
5931
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
6032
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
@@ -63,13 +35,6 @@
6335
</ClCompile>
6436
</ItemGroup>
6537
<ItemGroup>
66-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\b1903a6858b741129d0a3829b3410c34\modules\leetcode-treenode-cpp\LeetCodeTreeNodeToString.ixx">
67-
<FileType>Document</FileType>
68-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
69-
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">false</ExcludedFromBuild>
70-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
71-
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</DeploymentContent>
72-
</ClCompile>
7338
<ClCompile Include="debugTreeNode.ixx">
7439
<FileType>Document</FileType>
7540
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup>
4-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\bfsTravelsal.ixx" />
5-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\freeTreeNode.ixx" />
6-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\parseLeetCodeBinaryTree.ixx" />
7-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\serializeTreeNode.ixx" />
8-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\traversalTreeNode.ixx" />
9-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\cfc2bdf7726b496bad82152ad88c5cc0\modules\leetcode-treenode-cpp\TreeNode.ixx" />
104
<ClCompile Include="test.cpp" />
115
<ClCompile Include="debugTreeNode.ixx" />
12-
<ClCompile Include="..\..\..\..\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.5\b1903a6858b741129d0a3829b3410c34\modules\leetcode-treenode-cpp\LeetCodeTreeNodeToString.ixx" />
136
<ClCompile Include="index.ixx" />
147
<ClCompile Include="EqualTreeNode.ixx" />
158
<ClCompile Include="HashTreeNode.ixx" />
169
<ClCompile Include="printTreeNode.ixx" />
10+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\bfsTravelsal.ixx" />
11+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\freeTreeNode.ixx" />
12+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\LeetCodeTreeNodeToString.ixx" />
13+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\parseLeetCodeBinaryTree.ixx" />
14+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\serializeTreeNode.ixx" />
15+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\traversalTreeNode.ixx" />
16+
<ClCompile Include="..\..\Users\Administrator\AppData\Local\.xmake\packages\l\leetcode-treenode-cpp\1.1.6\211379a6b5ca44ac96c02258817ddd07\modules\leetcode-treenode-cpp\TreeNode.ixx" />
17+
<ClCompile Include="PostOrderTraversal.ixx" />
1718
</ItemGroup>
1819
</Project>

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import leetcode_treenode_cpp.parseLeetCodeBinaryTree;
3737
import insert_into_a_binary_search_tree.Solution;
3838
import insert_into_a_binary_search_tree.HashTreeNode;
3939
import insert_into_a_binary_search_tree.EqualTreeNode;
40+
import insert_into_a_binary_search_tree.PostOrderTraversal;
4041
using namespace insert_into_a_binary_search_tree;
4142
using namespace leetcode_treenode_cpp;
4243
using namespace std;
@@ -67,19 +68,24 @@ void test1()
6768
println("insert-into-a-binary-search-tree");
6869
println("test1 start");
6970
TreeNode* none = NULL;
71+
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> {};
7072

73+
PostOrderTraversal(none, [&](auto* node) {
74+
nodes.emplace(node);
75+
});
7176
auto result2 = Solution().insertIntoBST(none, 111);
72-
77+
PostOrderTraversal(result2, [&](auto* node) {
78+
nodes.emplace(node);
79+
});
7380
println(serializeTreeNode(none));
7481
assertEquals(serializeTreeNode(none), "null");
7582
println(serializeTreeNode(result2));
7683
assertEquals(serializeTreeNode(result2),
7784
"TreeNode{val:111,left:null,right:null}");
7885

79-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { none, result2 };
8086
for (auto node : nodes) {
8187
printTreeNode(node);
82-
freeTreeNode(node);
88+
delete (node);
8389
}
8490
println("test1 end");
8591
}
@@ -88,20 +94,26 @@ void test2()
8894
{
8995
println("test2 start");
9096
auto tree = new TreeNode(99);
97+
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> {};
98+
99+
PostOrderTraversal(tree, [&](auto* node) {
100+
nodes.emplace(node);
101+
});
91102
println(serializeTreeNode(tree));
92103
assertEquals(serializeTreeNode(tree),
93104
"TreeNode{val:99,left:null,right:null}");
94105
auto result = Solution().insertIntoBST(tree, 111);
95-
106+
PostOrderTraversal(result, [&](auto* node) {
107+
nodes.emplace(node);
108+
});
96109
println(serializeTreeNode(result));
97110
assertEquals(serializeTreeNode(result),
98111
"TreeNode{val:99,left:null,right:TreeNode{val:111,left:null,"
99112
"right:null}}");
100113

101-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { tree, result };
102114
for (auto node : nodes) {
103115
printTreeNode(node);
104-
freeTreeNode(node);
116+
delete (node);
105117
}
106118
println("test2 end");
107119
}
@@ -134,17 +146,23 @@ class StringTest : public CppUnit::TestFixture {
134146
TreeNode* root = nullptr;
135147
int status = parseLeetCodeBinaryTree(example.root, &root);
136148
CPPUNIT_ASSERT_EQUAL(0, status);
137-
auto output = Solution().insertIntoBST(root, example.val);
149+
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> {};
138150

151+
PostOrderTraversal(root, [&](auto* node) {
152+
nodes.emplace(node);
153+
});
154+
auto output = Solution().insertIntoBST(root, example.val);
155+
PostOrderTraversal(output, [&](auto* node) {
156+
nodes.emplace(node);
157+
});
139158
CPPUNIT_ASSERT_EQUAL(LeetCodeTreeNodeToString(output),
140159
example.output);
141160
println(example.root);
142161
println(example.val);
143162
println(example.output);
144-
auto nodes = unordered_set<TreeNode*, HashTreeNode, EqualTreeNode> { root, output };
145163
for (auto node : nodes) {
146164
printTreeNode(node);
147-
freeTreeNode(node);
165+
delete (node);
148166
}
149167
}
150168
}

0 commit comments

Comments
 (0)