Skip to content

Commit 7085441

Browse files
refactor 285
1 parent 6ca9dcb commit 7085441

File tree

1 file changed

+56
-20
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+56
-20
lines changed

src/main/java/com/fishercoder/solutions/_285.java

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,71 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.Iterator;
6+
import java.util.Map;
7+
import java.util.TreeMap;
8+
59
/**285. Inorder Successor in BST
610
711
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
812
913
Note: If the given node has no in-order successor in the tree, return null. */
1014
public class _285 {
1115

12-
/**credit: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative
13-
The inorder traversal of a BST is the nodes in ascending order.
14-
To find a successor, you just need to find the smallest one that is larger than the given value since there are no duplicate values in a BST.
15-
It's just like the binary search in a sorted list.
16-
17-
The time complexity should be O(h) where h is the depth of the result node.
18-
succ is a pointer that keeps the possible successor.
19-
Whenever you go left the current root is the new possible successor, otherwise the it remains the same.
20-
21-
Only in a balanced BST O(h) = O(log n). In the worst case h can be as large as n.
22-
*/
23-
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
24-
TreeNode successor = null;
25-
while (root != null) {
26-
if (p.val < root.val) {
27-
successor = root;
28-
root = root.left;
29-
} else {
30-
root = root.right;
16+
public static class Solution1 {
17+
/**
18+
* credit: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative
19+
* The inorder traversal of a BST is the nodes in ascending order.
20+
* To find a successor, you just need to find the smallest one that is larger than the given value since there are no duplicate values in a BST.
21+
* It's just like the binary search in a sorted list.
22+
*

23+
* The time complexity should be O(h) where h is the depth of the result node.
24+
* succ is a pointer that keeps the possible successor.
25+
* Whenever you go left the current root is the new possible successor, otherwise the it remains the same.
26+
*

27+
* Only in a balanced BST O(h) = O(log n). In the worst case h can be as large as n.
28+
*/
29+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
30+
TreeNode successor = null;
31+
while (root != null) {
32+
if (p.val < root.val) {
33+
successor = root;
34+
root = root.left;
35+
} else {
36+
root = root.right;
37+
}
38+
}
39+
return successor;
40+
}
41+
}
42+
43+
public static class Solution2 {
44+
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
45+
TreeMap<Integer, TreeNode> map = new TreeMap<>();
46+
inorderTraversal(root, map);
47+
Iterator<Map.Entry<Integer, TreeNode>> iterator = map.entrySet().iterator();
48+
while (iterator.hasNext()) {
49+
Map.Entry<Integer, TreeNode> entry = iterator.next();
50+
if (entry.getValue() == p) {
51+
if (iterator.hasNext()) {
52+
return iterator.next().getValue();
53+
} else {
54+
return null;
55+
}
56+
}
57+
}
58+
return null;
59+
}
60+
61+
private void inorderTraversal(TreeNode root, TreeMap<Integer, TreeNode> map) {
62+
if (root == null) {
63+
return;
3164
}
65+
inorderTraversal(root.left, map);
66+
map.put(root.val, root);
67+
inorderTraversal(root.right, map);
68+
return;
3269
}
33-
return successor;
3470
}
3571

3672
}

0 commit comments

Comments
 (0)