TreeNode* mistake1;
TreeNode* mistake2;
TreeNode* zszz=NULL;
//中序遍历正常情况下为升序排列,出现降序排列即为错误,那就交换两个降序的错误节点;
void dfslTree(TreeNode* root){
if(root= =NULL)
return ;
if(root->left!= NULL)
dfslTree(root->left);
if(zszz!=NULL && zszz->val>root->val)
{
if(mistake1==NULL)
{
mistake1 = zszz;
mistake2 = root;//交换第一次降序val值较大的错误节点和第二次降序val值较小的错误节点;
}
else
mistake2 = root;
}
zszz= root;
if(root->right!=NULL)
dfslTree(root->right);
}
void recoverTree(TreeNode* root) {
dfsTree(root);
int temp = mistake1->val;
mistake1->val = mistake2->val;
mistake2->val = temp;
}