mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 02:56:29 +08:00
26 lines
576 B
Go
26 lines
576 B
Go
// File: preorder_traversal_ii_compact.go
|
|
// 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)
|
|
if root.Val.(int) == 7 {
|
|
// 记录解
|
|
*res = append(*res, append([]*TreeNode{}, *path...))
|
|
}
|
|
preOrderII(root.Left, res, path)
|
|
preOrderII(root.Right, res, path)
|
|
// 回退
|
|
*path = (*path)[:len(*path)-1]
|
|
}
|