-
Notifications
You must be signed in to change notification settings - Fork 0
/
RangeSumBST.kt
177 lines (144 loc) · 4.58 KB
/
RangeSumBST.kt
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
/*
* Range Sum of BST https://leetcode.com/problems/range-sum-of-bst/
Given the root node of a binary search tree and two integers low and high,
return the sum of values of all nodes with a value in the inclusive range [low, high].
Example 1:
Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.
Example 2:
Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.
Constraints:
The number of nodes in the tree is in the range [1, 2 * 104].
1 <= Node.val <= 105
1 <= low <= high <= 105
All Node.val are unique.
*/
/**
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
return root?.let {
rangeSumBST(it.left, low, high) +
rangeSumBST(it.right, low, high) +
if(it.`val` < low || it.`val` > high) 0
else it.`val`
} ?: 0
}
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
if (root == null) return 0
val v = root.`val`
return when {
v < low -> rangeSumBST(root.right, low, high)
v > high -> rangeSumBST(root.left, low, high)
else -> v +
rangeSumBST(root.left, low, high) +
rangeSumBST(root.right, low, high)
}
}
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
var sum = 0
fun dfs(root: TreeNode?) {
root ?: return
if (root.`val` in low..high) sum += root.`val`
dfs(root.left)
dfs(root.right)
}
dfs(root)
return sum
}
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
return walk(root, low, high, 0)
}
private fun walk(node: TreeNode?, low: Int, high: Int, sumSoFar: Int): Int {
if (node == null) return -1
val current = node.`val`
if (current < low) {
return walk(node.right, low, high, sumSoFar)
} else if (current > high) {
return walk(node.left, low, high, sumSoFar)
} else {
val left = walk(node.left, low, high, sumSoFar)
val right = walk(node.right, low, high, sumSoFar)
var result = sumSoFar
if (left != -1) {
result += left
}
if (right != -1) {
result += right
}
return result + current
}
}
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int = mutableListOf(root!!).let { st ->
generateSequence(0) { _ ->
val n = st.removeLast()
n.left != null && st.add(n.left!!)
n.right != null && st.add(n.right!!)
if (n.`val` in low..high) n.`val` else 0
}.takeWhile { st.isNotEmpty() }.sum()
}
tailrec fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
root ?: return 0
val l = if (root.`val` > low)
rangeSumBST(root.left, low, high)
else
0
val r = if (root.`val` < high)
rangeSumBST(root.right, low, high)
else
0
val d = if (root.`val` in low..high)
root.`val`
else
0
return l + r + d
}
class Solution {
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
if (root == null) return 0
var result = 0
queue.add(root)
while (queue.isNotEmpty()) {
val node = queue.removeFirst()
val value = node.`val`
if (value >= low && value <= high) result += value
if (value > low) node.left?.let { queue.addLast(it) }
if (value < high) node.right?.let { queue.addLast(it) }
}
return result
}
companion object { val queue = LinkedList<TreeNode>() }
}
fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
var result = 0
var index = 0
array[index++] = root ?: empty
while (index > 0) {
val node = array[--index]
result += if (node.`val` in low..high) node.`val` else 0
if (node.`val` > low) node.left?.let { array[index++] = it }
if (node.`val` < high) node.right?.let { array[index++] = it }
}
return result
}
companion object {
val empty = TreeNode(0)
val array = Array<TreeNode>(500) { empty }
}
class Solution {
tailrec fun rangeSumBST(root: TreeNode?, low: Int, high: Int): Int {
root ?: return 0
val sum = if (root.`val` in low..high) root.`val` else 0
return sum + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high)
}
}