点击打开链接
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void helper(TreeNode* root, TreeNode*& pre, vector<TreeNode*>&res){
if(root==NULL) return;
helper(root->left, pre, res);
if(pre != NULL && pre->val > root->val){ //前值 大于 中值
res.push_back(pre);
res.push_back(root);
}
pre = root; //
helper(root->right, pre, res);
}
void recoverTree(TreeNode* root) {
vector<TreeNode*> res;
TreeNode* pre = NULL;
helper(root, pre, res);
if(res.size() >0){
if(res.size()==2) swap(res[0]->val, res[1]->val);
if(res.size()==3) swap(res[0]->val, res[2]->val);
if(res.size()==4) swap(res[0]->val, res[3]->val);
}
}
};
![](https://i-blog.csdnimg.cn/blog_migrate/d95dee2d9d92d4571c146b2399832d64.png)