forked from havanagrawal/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_285.java
72 lines (63 loc) · 2.63 KB
/
_285.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
/**285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null. */
public class _285 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/25698/java-python-solution-o-h-time-and-o-1-space-iterative
* The inorder traversal of a BST is the nodes in ascending order.
* 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.
* It's just like the binary search in a sorted list.
* <p>
* The time complexity should be O(h) where h is the depth of the result node.
* succ is a pointer that keeps the possible successor.
* Whenever you go left the current root is the new possible successor, otherwise the it remains the same.
* <p>
* Only in a balanced BST O(h) = O(log n). In the worst case h can be as large as n.
*/
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeNode successor = null;
while (root != null) {
if (p.val < root.val) {
successor = root;
root = root.left;
} else {
root = root.right;
}
}
return successor;
}
}
public static class Solution2 {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
TreeMap<Integer, TreeNode> map = new TreeMap<>();
inorderTraversal(root, map);
Iterator<Map.Entry<Integer, TreeNode>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, TreeNode> entry = iterator.next();
if (entry.getValue() == p) {
if (iterator.hasNext()) {
return iterator.next().getValue();
} else {
return null;
}
}
}
return null;
}
private void inorderTraversal(TreeNode root, TreeMap<Integer, TreeNode> map) {
if (root == null) {
return;
}
inorderTraversal(root.left, map);
map.put(root.val, root);
inorderTraversal(root.right, map);
return;
}
}
}