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.
92 lines
2.1 KiB
92 lines
2.1 KiB
// 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 := SliceToTree([]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 := SliceToTree([]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 := SliceToTree([]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 := SliceToTree([]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()
|
|
}
|
|
}
|