mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
39 lines
643 B
Go
39 lines
643 B
Go
|
package main
|
||
|
|
||
|
import "slices"
|
||
|
|
||
|
func delNodes(root *TreeNode, to_delete []int) []*TreeNode {
|
||
|
var forest []*TreeNode
|
||
|
|
||
|
var delNodesRec func(*TreeNode) *TreeNode
|
||
|
delNodesRec = func(node *TreeNode) *TreeNode {
|
||
|
if node == nil {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
node.Left = delNodesRec(node.Left)
|
||
|
node.Right = delNodesRec(node.Right)
|
||
|
|
||
|
if slices.Contains(to_delete, node.Val) {
|
||
|
if node.Left != nil {
|
||
|
forest = append(forest, node.Left)
|
||
|
}
|
||
|
|
||
|
if node.Right != nil {
|
||
|
forest = append(forest, node.Right)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return node
|
||
|
}
|
||
|
|
||
|
root = delNodesRec(root)
|
||
|
if root != nil {
|
||
|
forest = append(forest, root)
|
||
|
}
|
||
|
|
||
|
return forest
|
||
|
}
|