34.二叉树中和为某一值的路径
Last updated
Last updated
func pathSum(_ root: TreeNode?, _ target: Int) -> [[Int]] {
guard let rootV = root?.val else {
return []
}
var result: [[Int]] = []
let deta = target - rootV
var temp: [TreeNode] = []
if let left = root?.left {
temp.append(left)
}
if let right = root?.right {
temp.append(right)
}
if temp.isEmpty { // 到达叶节点了
if target == rootV {
result.append([rootV])
}
}
else {
temp.forEach { tree in
pathSum(tree, deta).forEach { datas in
result.append([rootV] + datas)
}
}
}
return result
}