Fix the code of preorder_traversal_iii_compact

This commit is contained in:
krahets
2023-07-21 22:08:26 +08:00
parent 075c3abf88
commit bba62bbe75
11 changed files with 19 additions and 10 deletions

View File

@@ -19,6 +19,7 @@ func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
if int(root.Val) == 7 {
// 记录解
*res = append(*res, *path)
*path = (*path)[:len(*path)-1]
return
}
preOrderIII(root.Left, res, path)

View File

@@ -59,7 +59,7 @@ func TestPreorderTraversalIIICompact(t *testing.T) {
res := make([][]*TreeNode, 0)
preOrderIII(root, &res, &path)
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
@@ -81,7 +81,7 @@ func TestPreorderTraversalIIITemplate(t *testing.T) {
choices = append(choices, root)
backtrackIII(&state, &choices, &res)
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点")
fmt.Println("\n输出所有根节点到节点 7 的路径,路径中不包含值为 3 的节点,仅包含一个值为 7 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)