-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathmerge-bsts-to-create-single-bst.cc
89 lines (87 loc) · 2.01 KB
/
merge-bsts-to-create-single-bst.cc
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
class Solution {
public:
TreeNode* canMerge(vector<TreeNode*>& ts) {
int n = ts.size();
unordered_map<int, TreeNode *> nroot;
for (TreeNode *t : ts) {
if (t->left)
nroot[t->left->val] = t->left;
if (t->right)
nroot[t->right->val] = t->right;
}
TreeNode *root = nullptr;
for (TreeNode *t : ts) {
auto it = nroot.find(t->val);
if (it == nroot.end()) {
if (root) return nullptr;
root = t;
continue;
}
it->second->left = t->left;
it->second->right = t->right;
}
int c = 0, v = INT_MIN;
for (auto *x = root; x; ) {
if (TreeNode *y = x->left) {
while (y->right && y->right != x)
y = y->right;
if (!y->right) {
y->right = x;
x = x->left;
continue;
}
y->right = nullptr;
}
if (v >= x->val) return nullptr;
v = x->val;
c++;
x = x->right;
}
return c == nroot.size()+1 ? root : nullptr;
}
};
///
class Solution {
public:
map<int, TreeNode *> mp;
bool ok;
tuple<TreeNode *, int, int> dfs(TreeNode *x) {
if (!x->left && !x->right) {
auto it = mp.find(x->val);
if (it != mp.end()) {
x = it->second;
mp.erase(it);
}
}
int mn = x->val, mx = x->val, tmp;
if (x->left) {
tie(x->left, mn, tmp) = dfs(x->left);
ok &= tmp < x->val;
}
if (x->right) {
tie(x->right, tmp, mx) = dfs(x->right);
ok &= x->val < tmp;
}
return {x, mn, mx};
}
TreeNode* canMerge(vector<TreeNode*>& ts) {
int n = ts.size();
unordered_set<int> nroot;
mp.clear();
for (TreeNode *t : ts) {
mp[t->val] = t;
if (t->left)
nroot.insert(t->left->val);
if (t->right)
nroot.insert(t->right->val);
}
for (TreeNode *t : ts)
if (!nroot.count(t->val)) {
ok = true;
mp.erase(t->val);
auto *ans = get<0>(dfs(t));
return ok && mp.empty() ? ans : nullptr;
}
return nullptr;
}
};