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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
<<-DOC
Note: Your solution should have only one BST traversal and O(1) extra space complexity,
since this is what you will be asked to accomplish in an interview.
A tree is considered a binary search tree (BST) if for each of its nodes the following is true:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and the right subtrees must also be binary search trees.
Given a binary search tree t, find the kth largest element in it.
Note that kth largest element means kth element in increasing order. See examples for better understanding.
Example
For
t = {
"value": 3,
"left": {
"value": 1,
"left": null,
"right": null
},
"right": {
"value": 5,
"left": {
"value": 4,
"left": null,
"right": null
},
"right": {
"value": 6,
"left": null,
"right": null
}
}
}
and k = 2, the output should be
kthLargestInBST(t, k) = 3.
Here is what t looks like:
3
/ \
1 5
/ \
4 6
The values of t are [1, 3, 4, 5, 6], and the 2nd element when the values are in sorted order is 3.
For
t = {
"value": 1,
"left": {
"value": -1,
"left": {
"value": -2,
"left": null,
"right": null
},
"right": {
"value": 0,
"left": null,
"right": null
}
},
"right": null
}
and k = 1, the output should be
kthLargestInBST(t, k) = -2.
Here is what t looks like:
1
/
-1
/ \
-2 0
The values of t are [-2, -1, 0, 1], and the 1st largest is -2.
Input/Output
[time limit] 4000ms (rb)
[input] tree.integer t
A tree of integers. It is guaranteed that t is a BST.
Guaranteed constraints:
1 ≤ tree size ≤ 104,
-105 ≤ node value ≤ 105.
[input] integer k
An integer.
Guaranteed constraints:
1 ≤ k ≤ tree size.
[output] integer
The kth largest value in t
DOC
describe "#kth_largest_in_bst" do
def kth_largest_in_bst(tree, k)
puts tree.to_a.inspect
stack = [tree]
all = [ ]
until stack.empty?
node = stack.pop
all.push(node.value)
stack.push(node.left) if node.left
stack.push(node.right) if node.right
end
puts all.inspect
all[k + 1]
end
def to_array(tree)
return [] if tree.nil?
to_array(tree.left) + [tree.value] + to_array(tree.right)
end
def kth_largest_in_bst(tree, k)
to_array(tree)[k - 1]
end
class Traversal
attr_reader :k
def initialize(k)
@counter = 0
@k = k
end
def traverse(tree)
return if tree.nil?
if @counter < k && result = traverse(tree.left)
return result
end
@counter += 1
return tree.value if @counter == k
traverse(tree.right) if @counter < k
end
end
def kth_largest_in_bst(tree, k)
Traversal.new(k).traverse(tree)
end
def traverse(node, yielder)
return if node.nil?
traverse(node.left, yielder)
yielder.yield node.value
traverse(node.right, yielder)
end
def kth_largest_in_bst(tree, k)
x = Enumerator.new { |yielder| traverse(tree, yielder) }
(k-1).times { x.next }
x.next
end
[
{ t: { "value": 3, "left": { "value": 1, "left": nil, "right": nil }, "right": { "value": 5, "left": { "value": 4, "left": nil, "right": nil }, "right": { "value": 6, "left": nil, "right": nil } } }, k: 2, x: 3 },
{ t: { "value": 1, "left": { "value": -1, "left": { "value": -2, "left": nil, "right": nil }, "right": { "value": 0, "left": nil, "right": nil } }, "right": nil }, k: 1, x: -2 },
{ t: { "value": 100, "left": nil, "right": nil }, k: 1, x: 100 },
{ t: { "value": 1, "left": { "value": 0, "left": nil, "right": nil }, "right": { "value": 2, "left": nil, "right": nil } }, k: 3, x: 2 },
{ t: { "value": 1, "left": { "value": 0, "left": nil, "right": nil }, "right": { "value": 2, "left": nil, "right": nil } }, k: 2, x: 1 },
{ t: { "value": -2, "left": nil, "right": { "value": 3, "left": { "value": 2, "left": nil, "right": nil }, "right": nil } }, k: 1, x: -2 }
].each do |x|
it do
expect(kth_largest_in_bst(Tree.build_from(x[:t]), x[:k])).to eql(x[:x])
end
end
end
|