Skip to content

Commit 4500bd8

Browse files
refactor 99
1 parent 7180c33 commit 4500bd8

File tree

1 file changed

+39
-33
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-33
lines changed
Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
package com.fishercoder.solutions;
22

33
import com.fishercoder.common.classes.TreeNode;
4-
/**Two elements of a binary search tree (BST) are swapped by mistake.
5-
6-
Recover the tree without changing its structure.
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.
79
810
Note:
9-
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?*/
11+
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
12+
*/
13+
1014
public class _99 {
11-
TreeNode firstElement = null;
12-
TreeNode secondElement = null;
15+
public static class Solution1 {
16+
TreeNode firstElement = null;
17+
TreeNode secondElement = null;
1318

14-
TreeNode prevElement = new TreeNode(Integer.MIN_VALUE);
19+
TreeNode prevElement = new TreeNode(Integer.MIN_VALUE);
1520

16-
public void recoverTree(TreeNode root) {
17-
traverseTree(root);
21+
public void recoverTree(TreeNode root) {
22+
traverseTree(root);
1823

19-
//swap the two elements
20-
int temp = firstElement.val;
21-
firstElement.val = secondElement.val;
22-
secondElement.val = temp;
23-
}
24+
//swap the two elements
25+
int temp = firstElement.val;
26+
firstElement.val = secondElement.val;
27+
secondElement.val = temp;
28+
}
2429

25-
private void traverseTree(TreeNode root) {
26-
if (root == null) {
27-
return;
28-
}
30+
private void traverseTree(TreeNode root) {
31+
if (root == null) {
32+
return;
33+
}
2934

30-
traverseTree(root.left);
35+
traverseTree(root.left);
3136

32-
//prevElement means the one previous to the current root, refer to in-order traversal, previous element must be smaller than the current root
33-
//if it's bigger, then we find the first element, thus we store it in the variable called firstElement
34-
if (firstElement == null && prevElement.val >= root.val) {
35-
firstElement = prevElement;
36-
}
37+
//prevElement means the one previous to the current root, refer to in-order traversal, previous element must be smaller than the current root
38+
//if it's bigger, then we find the first element, thus we store it in the variable called firstElement
39+
if (firstElement == null && prevElement.val >= root.val) {
40+
firstElement = prevElement;
41+
}
3742

38-
if (firstElement != null && prevElement.val >= root.val) {
39-
secondElement = root;
40-
}
43+
if (firstElement != null && prevElement.val >= root.val) {
44+
secondElement = root;
45+
}
4146

42-
//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
43-
//since the current root will be the new previous node for the right subtree.
44-
prevElement = root;
47+
//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
48+
//since the current root will be the new previous node for the right subtree.
49+
prevElement = root;
4550

46-
traverseTree(root.right);
47-
}
51+
traverseTree(root.right);
52+
}
4853

49-
}
54+
}
55+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy