-
Notifications
You must be signed in to change notification settings - Fork 0
/
200130-1.cpp
120 lines (113 loc) · 2.34 KB
/
200130-1.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
#include <cstdio>
#include <vector>
#include <initializer_list>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* create(initializer_list<int> a)
{
if (a.size() == 0) return NULL;
auto it = a.begin();
if (*it <= 0) return NULL;
TreeNode* t = new TreeNode(*it);
//printf("create root (%d)\n", t->val);
vector<TreeNode*> parents{t};
vector<TreeNode*> children;
++it;
for (size_t i = 0; it != a.end();) {
//printf("add : %d, i = %d\n", *it, i);
if (i < parents.size() * 2) {
TreeNode* c = (*it <= 0 ? NULL : new TreeNode(*it));
TreeNode* n = parents[i / 2];
if (i % 2 == 0) {
//if (c) printf("add (%d) to (%d)'s left\n", c->val, n->val);
n->left = c;
} else {
//if (c) printf("add (%d) to (%d)'s right\n", c->val, n->val);
n->right = c;
}
if (c) children.push_back(c);
++i;
++it;
} else {
parents = children;
children.clear();
i = 0;
}
}
return t;
}
void release(TreeNode* t)
{
if (t) {
release(t->left);
release(t->right);
delete t;
}
}
void print(TreeNode* t, int space = 10)
{
if (t == NULL) { printf("NULL\n"); return; }
vector<TreeNode*> parent{t};
vector<TreeNode*> children;
for (;;) {
bool allNull = true;
for (size_t i = 0; i < parent.size(); ++i) {
if (parent[i]) { allNull = false; break; }
}
if (allNull) break;
printf("%*s", space, "");
for (size_t i = 0; i < parent.size(); ++i) {
if (parent[i]) {
printf(" %3d ", parent[i]->val);
children.push_back(parent[i]->left);
children.push_back(parent[i]->right);
} else {
printf(" nul ");
children.push_back(NULL);
children.push_back(NULL);
}
}
printf("\n");
parent = children;
children.clear();
space -= 3;
}
}
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> a;
visit(root, a);
return a;
}
private:
void visit(TreeNode* t, vector<int>& a) {
if (t) {
visit(t->left, a);
a.push_back(t->val);
visit(t->right, a);
}
}
};
void print(const vector<int>& a)
{
printf("[ ");
for (auto e : a) { printf("%d ", e); }
printf("]\n");
}
int main()
{
Solution s;
{
TreeNode* t = create({1,-1,2,3});
//print(t);
print(s.inorderTraversal(t)); // answer: [1,3,2]
}
return 0;
}