题解 | #恢复二叉搜索树#
恢复二叉搜索树
http://www.nowcoder.com/practice/67c7172122b54b748e78eac7b183b5f3
class Solution {
private TreeNode t1,t2,pre;
public void recoverTree(TreeNode root) {
inOrder(root);
int temp = t1.val;
t1.val = t2.val;
t2.val = temp;
}
public void inOrder(TreeNode root){
if(root == null)
return;
inOrder(root.left);
if(pre!=null && pre.val > root.val){
if(t1==null)
t1 = pre;
t2 = root;
}
pre = root;
inOrder(root.right);
}
}
查看26道真题和解析