Skip to content

Instantly share code, notes, and snippets.

@Jocelyn9
Last active August 29, 2015 14:07
Show Gist options
  • Save Jocelyn9/4b23b6f3b5ca1be13ea9 to your computer and use it in GitHub Desktop.
Save Jocelyn9/4b23b6f3b5ca1be13ea9 to your computer and use it in GitHub Desktop.
class Solution {
TreeNode a = null, b = null, pre = null;
public void recoverTree(TreeNode root) {
if (root == null)
return;
recover(root);
TreeNode temp = new TreeNode(0);
temp.val = a.val;
a.val = b.val;
b.val = temp.val;
} // end
public void recover(TreeNode root) {
if(root==null) return ;
recover(root.left);
if(pre!=null && pre.val > root.val) {
if(a==null){
a = pre;
b = root;
}else{
b = root;
}
}
pre = root;
recover(root.right);
} // end
} // end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment