### 题目：

1
/
3
\
2

3
/
1
\
2

3
/ \
1   4
/
2

2
/ \
1   4
/
3

使用 O(n) 空间复杂度的解法很容易实现。
你能想出一个只使用常数空间的解决方案吗？

### 解题：

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/

int midOrder(struct TreeNode **pre, struct TreeNode **err1, struct TreeNode **err2, struct TreeNode *root)
{
if (root == NULL) return 0;

if (midOrder(pre, err1, err2, root->left) == -1) return -1;
if (*pre != NULL && (*pre)->val > root->val) {
if (*err1 == NULL) {
*err1 = *pre;
*err2 = root;
} else {
*err2 = root;
return -1;
}
}
*pre = root;
if (midOrder(pre, err1, err2, root->right) == -1) return -1;
return 0;
}

void recoverTree(struct TreeNode* root){
struct TreeNode *pre = NULL;
struct TreeNode * err1 = NULL;
struct TreeNode * err2 = NULL;
midOrder(&pre, &err1, &err2, root);

int tmp;
tmp = err1->val;
err1->val = err2->val;
err2->val = tmp;
return;
}

01-04 195
07-04 1万+

09-30 144
09-08 191
05-06 4196
08-07 2万+
11-17 6615