1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00
LeetCode/kt/delete-leaves-with-a-given-value.kt
Matej Focko 04c2005f8a
kt: add «1325. Delete Leaves With a Given Value»
Signed-off-by: Matej Focko <me@mfocko.xyz>
2024-05-17 18:26:09 +02:00

28 lines
618 B
Kotlin

class TreeNode(var `val`: Int) {
var left: TreeNode? = null
var right: TreeNode? = null
}
class Solution {
fun isLeaf(node: TreeNode?): Boolean {
return node != null && node.left == null && node.right == null
}
fun removeLeafNodes(
root: TreeNode?,
target: Int,
): TreeNode? {
if (root == null) {
return null
}
root.left = removeLeafNodes(root.left, target)
root.right = removeLeafNodes(root.right, target)
if (isLeaf(root) && root.`val` == target) {
return null
}
return root
}
}