-
Notifications
You must be signed in to change notification settings - Fork 0
/
200204-1.cpp
136 lines (126 loc) · 2.56 KB
/
200204-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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
#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);
vector<TreeNode*> parents{t};
vector<TreeNode*> children;
++it;
for (size_t i = 0; it != a.end();) {
if (i < parents.size() * 2) {
TreeNode* c = (*it <= 0 ? NULL : new TreeNode(*it));
TreeNode* n = parents[i / 2];
if (i % 2 == 0) { n->left = c; } else { 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)
{
vector<TreeNode*> a{t};
int last = 0;
for (int i = 0;;) {
int end = a.size();
for (; i < end; ++i) {
a.push_back(a[i] ? a[i]->left : NULL);
a.push_back(a[i] ? a[i]->right : NULL);
if (a[i]) {
if (a[i]->right) last = a.size() - 1;
else if (a[i]->left) last = a.size() - 2;
}
}
if (last < end) break;
}
a.resize(last + 1);
printf("[ ");
for (auto e : a) {
if (e) {
printf("%d ", e->val);
} else {
printf("null ");
}
}
printf("]\n");
}
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* init(initializer_list<int> a) {
ListNode node(0);
ListNode* p = &node;
for (auto e : a) {
p->next = new ListNode(e);
p = p->next;
}
return node.next;
}
void release(ListNode* p) {
if (p) release(p->next);
delete p;
}
void print(ListNode* p, bool hasEndl = true) {
if (p) {
printf("%d ", p->val);
if (p->next) print(p->next, false);
}
if (hasEndl) printf("\n");
}
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
int n = 0;
for (ListNode* p = head; p; p = p->next) ++n;
if (n <= 0) return NULL;
return sortedListToBST(head, n);
}
private:
TreeNode* sortedListToBST(ListNode* p, int n) {
if (n <= 0) return NULL;
ListNode* q = p;
int i = 0;
for (; i < n / 2; ++i) q = q->next;
TreeNode* t = new TreeNode(q->val);
if (q != p) t->left = sortedListToBST(p, i);
if (q->next) t->right = sortedListToBST(q->next, n - i - 1);
return t;
}
};
int main()
{
Solution s;
{
ListNode* n = init({-10, -3, 0, 5, 9});
TreeNode* t = s.sortedListToBST(n);
print(t);
release(t);
}
return 0;
}