You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
582 B
28 lines
582 B
// File: preorder_traversal_iii_compact.go
|
|
// Created Time: 2023-05-09
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
package chapter_backtracking
|
|
|
|
import (
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
)
|
|
|
|
/* 前序遍历:例题三 */
|
|
func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
|
|
// 剪枝
|
|
if root == nil || root.Val == 3 {
|
|
return
|
|
}
|
|
// 尝试
|
|
*path = append(*path, root)
|
|
if root.Val.(int) == 7 {
|
|
// 记录解
|
|
*res = append(*res, *path)
|
|
}
|
|
preOrderIII(root.Left, res, path)
|
|
preOrderIII(root.Right, res, path)
|
|
// 回退
|
|
*path = (*path)[:len(*path)-1]
|
|
}
|