-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
46 lines (43 loc) · 941 Bytes
/
Solution.java
File metadata and controls
46 lines (43 loc) · 941 Bytes
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
package com.q0099;
import com.q0101_symmetric_tree.TreeNode;
/**
* @author xjn
* @since 2020-06-07
* https://leetcode-cn.com/problems/recover-binary-search-tree/
* 99. 恢复二叉搜索树
*/
public class Solution {
private TreeNode pre;
private TreeNode a,b;
public void recoverTree(TreeNode root) {
if(root == null){
return;
}
pre = null;
a = null;
b = null;
inorder(root);
int c = a.val;
a.val = b.val;
b.val = c;
}
// 2
//1 4
// 3
private void inorder(TreeNode root){
if(root == null){
return;
}
inorder(root.left);
if(pre != null && root.val <= pre.val){
if(a == null){
a = pre;
b = root;
}else{
b = root;
}
}
pre = root;
inorder(root.right);
}
}