2023-07-26 03:15:49 +08:00
|
|
|
// File: preorder_traversal_ii_compact.go
|
2023-05-15 01:17:42 +08:00
|
|
|
// Created Time: 2023-05-09
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
package chapter_backtracking
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* 前序遍历:例题二 */
|
|
|
|
func preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
|
|
|
|
if root == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 尝试
|
|
|
|
*path = append(*path, root)
|
2023-07-26 11:04:09 +08:00
|
|
|
if root.Val.(int) == 7 {
|
2023-05-15 01:17:42 +08:00
|
|
|
// 记录解
|
2023-12-16 18:52:28 +08:00
|
|
|
*res = append(*res, append([]*TreeNode{}, *path...))
|
2023-05-15 01:17:42 +08:00
|
|
|
}
|
|
|
|
preOrderII(root.Left, res, path)
|
|
|
|
preOrderII(root.Right, res, path)
|
|
|
|
// 回退
|
|
|
|
*path = (*path)[:len(*path)-1]
|
|
|
|
}
|