hello-algo/codes/go/chapter_backtracking/preorder_traversal_test.go
Reanon a6b3f72826
feat(go/backtracking): add go code (#488)
* feat(go/backtracking): add go code

* feat(backtracking): add n_queens in go

* feat(backtracking): add /preorder_traversal_i_compact in go

* feat(backtracking): add /preorder_traversal_ii_compact in go

* feat(backtracking): add /preorder_traversal_ii_template in go

* feat(backtracking): add preorder_traversal_iii_compact in go

* feat(backtracking): add preorder_traversal_test in go

* feat(backtracking): add permutations_i in go

* feat(backtracking): add permutations_ii in go

* feat(backtracking): add permutation_test in go

* feat(backtracking): fix bug in go

* Update permutations_i.go

---------

Co-authored-by: Yudong Jin <krahets@163.com>
2023-05-15 01:17:42 +08:00

91 lines
2.1 KiB
Go

// File: preorder_traversal_i_compact_test.go
// Created Time: 2023-05-09
// Author: Reanon (793584285@qq.com)
package chapter_backtracking
import (
"fmt"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestPreorderTraversalICompact(t *testing.T) {
/* 初始化二叉树 */
root := ArrToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\n初始化二叉树")
PrintTree(root)
// 前序遍历
res := make([]*TreeNode, 0)
preOrderI(root, &res)
fmt.Println("\n输出所有值为 7 的节点")
for _, node := range res {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
func TestPreorderTraversalIICompact(t *testing.T) {
/* 初始化二叉树 */
root := ArrToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\n初始化二叉树")
PrintTree(root)
// 前序遍历
path := make([]*TreeNode, 0)
res := make([][]*TreeNode, 0)
preOrderII(root, &res, &path)
fmt.Println("\n输出所有根节点到节点 7 的路径")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}
func TestPreorderTraversalIIICompact(t *testing.T) {
/* 初始化二叉树 */
root := ArrToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\n初始化二叉树")
PrintTree(root)
// 前序遍历
path := make([]*TreeNode, 0)
res := make([][]*TreeNode, 0)
preOrderIII(root, &res, &path)
fmt.Println("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}
func TestPreorderTraversalIIITemplate(t *testing.T) {
/* 初始化二叉树 */
root := ArrToTree([]any{1, 7, 3, 4, 5, 6, 7})
fmt.Println("\n初始化二叉树")
PrintTree(root)
// 回溯算法
res := make([][]*TreeNode, 0)
state := make([]*TreeNode, 0)
choices := make([]*TreeNode, 0)
choices = append(choices, root)
backtrackIII(&state, &choices, &res)
fmt.Println("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点")
for _, path := range res {
for _, node := range path {
fmt.Printf("%v ", node.Val)
}
fmt.Println()
}
}