-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderStatisticsBst.java
More file actions
81 lines (69 loc) · 2.17 KB
/
OrderStatisticsBst.java
File metadata and controls
81 lines (69 loc) · 2.17 KB
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
73
74
75
76
77
78
79
80
81
class Solution {
//region precoditions
class TreeNodeWithSize {
int val;
int size;
TreeNodeWithSize left;
TreeNodeWithSize right;
TreeNodeWithSize(int val) { this.val = val; }
}
class OrderStatisticsTree {
TreeNodeWithSize root;
public int size(TreeNodeWithSize node){
if(node == null){
return 0;
}
return node.size;
}
public void put(TreeNodeWithSize node){
root = put(root, node);
}
private TreeNodeWithSize put(TreeNodeWithSize x, TreeNodeWithSize node){
if(x == null) {
node.size = 1;
return node;
}
if(node.val < x.val){
x.left = put(x.left, node);
}else if(node.val > x.val){
x.right = put(x.right, node);
}else{
x.val = node.val; //redundant, but leaving it here for display purposes
}
x.size = 1 + size(x.left) + size(x.right);
return x;
}
public int findByRank(int rank){
return findByRank(root, rank);
}
private int findByRank(TreeNodeWithSize root, int rank){
if(root == null) throw new IllegalArgumentException("empty tree");
int leftSize = 0;
if(root.left != null){
leftSize = root.left.size;
}
if(rank <= leftSize){
return findByRank(root.left, rank);
}else if(rank > leftSize + 1){
return findByRank(root.right, rank - leftSize - 1);
}
return root.val;
}
}
//endregion
//region problem solving
OrderStatisticsTree bst = new OrderStatisticsTree();
public int kthSmallest(TreeNode root, int k) {
buildOrderStatisticsTree(root);
return bst.findByRank(k);
}
//endregion
public void buildOrderStatisticsTree(TreeNode node) {
if (node == null){
return;
}
bst.put(new TreeNodeWithSize(node.val));
buildOrderStatisticsTree(node.left);
buildOrderStatisticsTree(node.right);
}
}