Skip to content

Commit fb8d00d

Browse files
refactor 99
1 parent e446fa2 commit fb8d00d

File tree

1 file changed

+30
-75
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-75
lines changed
Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,46 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4-
/**
5-
* 99. Recover Binary Search Tree
6-
*
7-
* Two elements of a binary search tree (BST) are swapped by mistake.
8-
* Recover the tree without changing its structure.
9-
10-
Example 1:
11-
12-
Input: [1,3,null,null,2]
13-
14-
1
15-
/
16-
3
17-
\
18-
2
19-
20-
Output: [3,1,null,null,2]
21-
22-
3
23-
/
24-
1
25-
\
26-
2
27-
Example 2:
28-
29-
Input: [3,1,4,null,null,2]
30-
31-
3
32-
/ \
33-
1 4
34-
/
35-
2
36-
37-
Output: [2,1,4,null,null,3]
38-
39-
2
40-
/ \
41-
1 4
42-
/
43-
3
44-
45-
Follow up:
46-
A solution using O(n) space is pretty straight forward.
47-
Could you devise a constant space solution?
48-
*/
494

505
public class _99 {
51-
public static class Solution1 {
52-
TreeNode firstElement = null;
53-
TreeNode secondElement = null;
6+
public static class Solution1 {
7+
TreeNode firstElement = null;
8+
TreeNode secondElement = null;
549

55-
TreeNode prevElement = new TreeNode(Integer.MIN_VALUE);
10+
TreeNode prevElement = new TreeNode(Integer.MIN_VALUE);
5611

57-
public void recoverTree(TreeNode root) {
58-
traverseTree(root);
12+
public void recoverTree(TreeNode root) {
13+
traverseTree(root);
5914

60-
//swap the two elements
61-
int temp = firstElement.val;
62-
firstElement.val = secondElement.val;
63-
secondElement.val = temp;
64-
}
15+
//swap the two elements
16+
int temp = firstElement.val;
17+
firstElement.val = secondElement.val;
18+
secondElement.val = temp;
19+
}
6520

66-
private void traverseTree(TreeNode root) {
67-
if (root == null) {
68-
return;
69-
}
21+
private void traverseTree(TreeNode root) {
22+
if (root == null) {
23+
return;
24+
}
7025

71-
traverseTree(root.left);
26+
traverseTree(root.left);
7227

73-
//prevElement means the one previous to the current root, refer to in-order traversal, previous element must be smaller than the current root
74-
//if it's bigger, then we find the first element, thus we store it in the variable called firstElement
75-
if (firstElement == null && prevElement.val >= root.val) {
76-
firstElement = prevElement;
77-
}
28+
//prevElement means the one previous to the current root, refer to in-order traversal, previous element must be smaller than the current root
29+
//if it's bigger, then we find the first element, thus we store it in the variable called firstElement
30+
if (firstElement == null && prevElement.val >= root.val) {
31+
firstElement = prevElement;
32+
}
7833

79-
if (firstElement != null && prevElement.val >= root.val) {
80-
secondElement = root;
81-
}
34+
if (firstElement != null && prevElement.val >= root.val) {
35+
secondElement = root;
36+
}
8237

83-
//this is the last step in the "do some business logic", so we'll always to have update the previous node to be the current root before it traverses the right subtree
84-
//since the current root will be the new previous node for the right subtree.
85-
prevElement = root;
38+
//this is the last step in the "do some business logic", so we'll always to have update the previous node to be the current root before it traverses the right subtree
39+
//since the current root will be the new previous node for the right subtree.
40+
prevElement = root;
8641

87-
traverseTree(root.right);
88-
}
42+
traverseTree(root.right);
43+
}
8944

90-
}
45+
}
9146
}

0 commit comments

Comments
 (0)