Question
Formatted question description: https://leetcode.ca/all/99.html
99 Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
@tag-tree
Algorithm
Three pointers are needed, first
and second
respectively represent the first and second disordered nodes, and pre
points to the previous node of the current node’s in-order traversal.
The traditional in-order traversal recursion is used here, but where the node value should be output, it is replaced by judging the size of pre and the current node value. If pre is larger, if first is empty, first
point to the node pointed to by pre
, or else, set second
to the current
node.
In this way, the in-order traverses the entire tree, and if both first and second exist, then their node values can be exchanged. The space complexity of this algorithm is still O(n), where n is the height of the tree.
Code
Java
public class Recover_Binary_Search_Tree {
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
TreeNode n1;
TreeNode n2;
TreeNode prev; // @note:@memorize: key is use prev!
public void recoverTree(TreeNode root) {
inOrderTraversal(root); // will record 2 swapped nodes
swap(); // swap 2 nodes
}
private void inOrderTraversal(TreeNode root) {
if (root == null) {
return;
}
inOrderTraversal(root.left);
// @note: 1,6,3,4,5,2 => NOT next to each other => n1=6-prev, but n2=2-root
// @note: 1,2,4,3,5,6 => when next to each other, n2 needs to be root
// bigger than next, then wrong
if (prev != null && prev.val >= root.val) {
if (n1 == null) {
n1 = prev;
n2 = root; // @note:@memorize: here is key, for case when in total two nodes
} else {
n2 = root;
}
}
prev = root;
inOrderTraversal(root.right);
}
// @note: concern about n1 or n2 is null? by question definition, if swapped, then at least 2 nodes
private void swap() {
int tmp = n1.val;
n1.val = n2.val;
n2.val = tmp;
}
}
}