Edgar Avuzi
11/08/2025, 5:52 AMEdgar Avuzi
11/08/2025, 5:56 AMEdgar Avuzi
11/08/2025, 6:03 AMclass Solution {
fun increasingBST(root: TreeNode?): TreeNode? {
val dummy = TreeNode(0)
var current = dummy
inorder(root) { node ->
node.left = null
current.right = node
current = node
}
return dummy.right
}
private fun inorder(node: TreeNode?, visit: (TreeNode) -> Unit) {
if (node == null) return
inorder(node.left, visit)
visit(node)
inorder(node.right, visit)
}
}Youssef Shoaib [MOD]
11/08/2025, 11:19 AMinorder tailrec so that the recursion on the right is unfolded into a loop.