Hello community,

my code is based on morris traversal. In the first traversal it should generate the larger node of that swap (big). In the second traversal once it encounters a node with value bigger than that larger node, the previous encountered node should be the smaller one of that swap. Then I swap them back.

I ran the code several times on my PC and no TLE at all, then I ran it on leetcode it got TLE so I really don't understand how come there can be a deadloop in the code...

Thanks for all the help.

Best,

```
public void recoverTree(TreeNode root) {
TreeNode node = root;
TreeNode left = null;
if (root==null){
return;
}
TreeNode pre = root;
TreeNode cur = root;
TreeNode big = root;
while (node!=null){
if (node.left==null){
pre = cur;
cur = node;
if (pre.val>cur.val) {
big = pre;
break;
}
node = node.right;
} else {
left = node.left;
while (left.right!=null&&left.right.val!=node.val){
left = left.right;
}
if (left.right==null){
left.right = node;
node = node.left;
} else {
left.right = null;
pre = cur;
cur = node;
if (pre.val>cur.val) {
big = pre;
break;
}
node = node.right;
}
}
}
while (node!=null){
if (node.left==null){
pre = cur;
cur = node;
if (cur.val>big.val) {
int tmp = big.val;
big.val = pre.val;
pre.val = tmp;
break;
}
node = node.right;
} else {
left = node.left;
while (left.right!=null&&left.right.val!=node.val){
left = left.right;
}
if (left.right==null){
left.right = node;
node = node.left;
} else {
left.right = null;
pre = cur;
cur = node;
if (cur.val>big.val) {
int tmp = big.val;
big.val = pre.val;
pre.val = tmp;
break;
}
node = node.right;
}
}
}
}
```