-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecoverBinarySearchTree.java
More file actions
121 lines (106 loc) · 3.01 KB
/
RecoverBinarySearchTree.java
File metadata and controls
121 lines (106 loc) · 3.01 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package hard;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import util.TreeNode;
/**
*
* ClassName: RecoverBinarySearchTree
* @author chenyiAlone
* Create Time: 2019/04/14 10:56:17
* Description: No.99
* 思路:
* 真是差点被这个题给搞死了,其实回头想想自己是真的菜,只要先序遍历整棵树,记录下两个非升序对的位置交换就可以了
* 1. TreeNode prev 表示前结点,
* a. if (prev != null && prev.val > cur.val) 将这个逆序对放入 list 中
* b. prev = cur;
* 2. 递归和非递归两种方式就没什么可说的的了
*
*
*
* Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Example 1:
Input: [1,3,null,null,2]
1
/
3
\
2
Output: [3,1,null,null,2]
3
/
1
\
2
Example 2:
Input: [3,1,4,null,null,2]
3
/ \
1 4
/
2
Output: [2,1,4,null,null,3]
2
/ \
1 4
/
3
Follow up:
A solution using O(n) space is pretty straight forward.
Could you devise a constant space solution?
*/
public class RecoverBinarySearchTree {
TreeNode prev = null;
List<TreeNode> res = new ArrayList<>();
public void recoverTreeNonRecursive(TreeNode root) {
// traversal(root);
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
if (!stack.isEmpty()) {
cur = stack.pop();
if (prev != null && prev.val > cur.val) {
res.add(prev);
res.add(cur);
}
prev = cur;
cur = cur.right;
}
}
int tmp = res.get(0).val;
res.get(0).val = res.get(res.size() - 1).val;
res.get(res.size() - 1).val = tmp;
}
public void recoverTree(TreeNode root) {
traversal(root);
int tmp = res.get(0).val;
res.get(0).val = res.get(res.size() - 1).val;
res.get(res.size() - 1).val = tmp;
}
void traversal(TreeNode root) {
if (root == null)
return;
traversal(root.left);
if (prev == null) {
prev = root;
} else {
if (prev.val > root.val) {
res.add(prev);
res.add(root);
}
prev = root;
}
traversal(root.right);
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(3);
root.left.right = new TreeNode(2);
System.out.println(root);
}
}