mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 09:59:41 +01:00
29 lines
618 B
Kotlin
29 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
|
||
|
}
|
||
|
}
|