-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
66 lines (61 loc) · 1.8 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <queue>
#include "treeParser.hpp"
std::string bfsTravelsal(TreeNode* root) {
std::string result = "[";
std::queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
if (node == nullptr) {
result += "null,";
} else {
result += std::to_string(node->val) + ",";
nodeQueue.push(node->left);
nodeQueue.push(node->right);
}
}
if (result == std::string("[null,")) {
return "[]";
}
result[result.size()-1] = ']';
return result;
}
int test(std::string& rawString) {
TreeNode* root = nullptr;
int status = parseLeetCodeBinaryTree(rawString, &root);
if (status != 0) {
return status;
}
std::string result = bfsTravelsal(root);
cleanTreeNode(root);
if (result != rawString) {
std::cout << "---------------------------------------" << std::endl;
std::cout << "input: " << rawString << std::endl;
std::cout << "output: " << result << std::endl;
}
return result == rawString ? 0 : -1;
}
std::string testcase[] = {
"[]",
"[1]",
"[1,null,2,3]",
"[5,1,4,null,null,3,6]",
"[1,3,null,null,2]",
"[3,1,4,null,null,2]",
"[1,2,5,3,4,null,6]",
"[1,2,5,3,4,null,6,null,null,null,7]",
"[5,4,8,11,null,13,4,7,2,null,null,null,1]",
"[1,null,2,null,3,null,4,null,5,null,6]"
};
int main() {
int status = 0;
for (int i = 0; i < sizeof(testcase)/sizeof(testcase[0]); i++) {
status += test(testcase[i]);
}
std::cout << "----------------------" << std::endl;
std::cout << "test fail (" << -status << "/" << sizeof(testcase)/sizeof(testcase[0]) << ")" << std::endl;
return 0;
}