Recover Binary Search Tree


算法:二叉搜索树, 深度优先搜索

url:https://leetcode.com/problems/recover-binary-search-tree/

题目

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
99. Recover Binary Search Tree
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?

分析

  1. 要求,不改变现有树结构的前提,使数恢复为二叉搜索树;
  2. 使用DFS便利二叉树,凡是 不符合规则的节点都标记出来;
  3. 中序遍历,遍历结果有序,无序的节点就是 不合格的节点

Java解法

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
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

class Solution {
TreeNode pre;
TreeNode first, second;
public void recoverTree(TreeNode root) {
pre = new TreeNode(Integer.MIN_VALUE);
inorder(root);
swap(first, second);
}
public void inorder(TreeNode root) {
if(null == root) return;
inorder(root.left);
if(pre.val >= root.val && pre.val != Integer.MIN_VALUE) {
if(null == first) first = pre;
second = root;
}
pre = root;
inorder(root.right);
}
public void swap(TreeNode node1, TreeNode node2) {
int tmp = node1.val;
node1.val = node2.val;
node2.val = tmp;
}
}

public class MainClass {
public static TreeNode stringToTreeNode(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return null;
}

String[] parts = input.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);

int index = 1;
while(!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();

if (index == parts.length) {
break;
}

item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int leftNumber = Integer.parseInt(item);
node.left = new TreeNode(leftNumber);
nodeQueue.add(node.left);
}

if (index == parts.length) {
break;
}

item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int rightNumber = Integer.parseInt(item);
node.right = new TreeNode(rightNumber);
nodeQueue.add(node.right);
}
}
return root;
}

public static String treeNodeToString(TreeNode root) {
if (root == null) {
return "[]";
}

String output = "";
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while(!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();

if (node == null) {
output += "null, ";
continue;
}

output += String.valueOf(node.val) + ", ";
nodeQueue.add(node.left);
nodeQueue.add(node.right);
}
return "[" + output.substring(0, output.length() - 2) + "]";
}

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
TreeNode root = stringToTreeNode(line);

new Solution().recoverTree(root);
String out = treeNodeToString(root);

System.out.print(out);
}
}
}

Python解法