-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBalanced.kt
More file actions
30 lines (27 loc) · 763 Bytes
/
isBalanced.kt
File metadata and controls
30 lines (27 loc) · 763 Bytes
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
package exercices.binarySearchTrees
import kotlin.math.abs
import kotlin.math.max
/**
* Checks if a binary search tree is balanced
* Complexity O(n)
*
* @param root Node<E>?
* @return Returns true if the binary search tree is balanced
*/
fun <E> isBalanced(root: Node<E>?): Boolean {
return isBalancedAux(root) != -1
}
/**
* Auxiliary function to check if a binary search tree is balanced
* Complexity O(n)
*
* @param root Node<E>?
* @return Returns the height of the binary search tree or -1 if it is not balanced
*/
fun <E> isBalancedAux(root: Node<E>?): Int {
if (root == null) return 0
val l = isBalancedAux(root.left)
val r = isBalancedAux(root.right)
if (l == -1 || r == -1) return -1
return if (abs(l - r) <= 1) max(l, r) + 1 else -1
}