-
Notifications
You must be signed in to change notification settings - Fork 0
/
776.cpp
61 lines (53 loc) · 1.33 KB
/
776.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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool dfs(TreeNode* n, int& max, int& min, TreeNode** parent)
{
trans = false;
max = n->val;
min = n->val;
int lmax = -1, lmin = 100;
int rmax = -1, rmin = 100;
if(n->left && dfs(n->left, lmax, lmin, &n))
{
return true;
}
if(n->left && lmax <= V && n->val > V)
{
ret.push_back(root);
ret.push_back(n->left);
return true;
}
if(n->right && dfs(n->right, rmax, rmin, &n))
{
return true;
}
//右子树链接破坏情形要复杂些,要嫁接
if(n->right && n->val <= V && n->right->val > V)
{
ret.push_back(root);
ret.push_back(n);
}
if(rmax > max)max = rmax;
if(lmin < min)min = lmin;
}
vector<TreeNode*> splitBST(TreeNode* root, int V) {
this->V = V;
this->root = root;
int min, max;
TreeNode** parent = NULL;
dfs(root, max, min, parent);
}
TreeNode* dest;
TreeNode* root;
int V;
vector<TreeNode*> ret;
};