forked from algorhythms/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
63 lines (56 loc) · 1.52 KB
/
Solution.java
File metadata and controls
63 lines (56 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package RecoverBinarySearchTree;
import commons.datastructures.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* User: Danyang
* Date: 1/27/2015
* Time: 21:30
*
* 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?
*/
public class Solution {
List<TreeNode> pair = new ArrayList<>(); // O(2)
TreeNode cur;
TreeNode pre;
/**
* Flatten to In-order array to do the analysis
* Global pointers
* Notice:
* 1. pointers
* 2. case when the 2 swapped are neighbor in in-order array
* 3. case when the 2 swapped are not neighbor in in-order array
* @param root
*/
public void recoverTree(TreeNode root) {
inorder(root);
assert pair.size()==2;
int t = pair.get(0).val;
pair.get(0).val = pair.get(1).val;
pair.get(1).val = t;
}
void inorder(TreeNode c) {
if(c==null)
return ;
inorder(c.left);
pre = cur;
cur = c;
if(pre != null) {
if(pre.val>cur.val) {
if(pair.size()==0) {
pair.add(pre);
pair.add(cur);
}
else {
pair.remove(pair.size()-1);
pair.add(cur);
}
assert pair.size()==2;
}
}
inorder(c.right);
}
}