-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path099.cpp
40 lines (38 loc) · 955 Bytes
/
099.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
/**
* 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:
// 获得中序遍历序列
void inorder(TreeNode *root, deque<int> &d)
{
if(root == NULL)
return ;
inorder(root->left, d);
d.push_back(root->val);
inorder(root->right, d);
}
// 将排序好以后的中序遍历序列重新写回BST中
void convert(TreeNode *root, deque<int> &d)
{
if(root == NULL)
return;
convert(root->left, d);
root->val = d.front();
d.pop_front();
convert(root->right, d);
}
void recoverTree(TreeNode* root) {
deque<int> d;
inorder(root, d);
// 对中序遍历序列进行排序
sort(d.begin(), d.end());
convert(root, d);
}
};