mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-13 15:56:05 +00:00
2778a6f9c7
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
92 lines
2.2 KiB
Go
92 lines
2.2 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) {
|
|
/* Initialize binary tree */
|
|
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
|
|
fmt.Println("\nInitialize binary tree")
|
|
PrintTree(root)
|
|
|
|
// Preorder traversal
|
|
res := make([]*TreeNode, 0)
|
|
preOrderI(root, &res)
|
|
|
|
fmt.Println("\nOutput all nodes with value 7")
|
|
for _, node := range res {
|
|
fmt.Printf("%v ", node.Val)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
|
|
func TestPreorderTraversalIICompact(t *testing.T) {
|
|
/* Initialize binary tree */
|
|
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
|
|
fmt.Println("\nInitialize binary tree")
|
|
PrintTree(root)
|
|
|
|
// Preorder traversal
|
|
path := make([]*TreeNode, 0)
|
|
res := make([][]*TreeNode, 0)
|
|
preOrderII(root, &res, &path)
|
|
|
|
fmt.Println("\nOutput all paths from root node to node 7")
|
|
for _, path := range res {
|
|
for _, node := range path {
|
|
fmt.Printf("%v ", node.Val)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func TestPreorderTraversalIIICompact(t *testing.T) {
|
|
/* Initialize binary tree */
|
|
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
|
|
fmt.Println("\nInitialize binary tree")
|
|
PrintTree(root)
|
|
|
|
// Preorder traversal
|
|
path := make([]*TreeNode, 0)
|
|
res := make([][]*TreeNode, 0)
|
|
preOrderIII(root, &res, &path)
|
|
|
|
fmt.Println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
|
|
for _, path := range res {
|
|
for _, node := range path {
|
|
fmt.Printf("%v ", node.Val)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|
|
|
|
func TestPreorderTraversalIIITemplate(t *testing.T) {
|
|
/* Initialize binary tree */
|
|
root := SliceToTree([]any{1, 7, 3, 4, 5, 6, 7})
|
|
fmt.Println("\nInitialize binary tree")
|
|
PrintTree(root)
|
|
|
|
// Backtracking algorithm
|
|
res := make([][]*TreeNode, 0)
|
|
state := make([]*TreeNode, 0)
|
|
choices := make([]*TreeNode, 0)
|
|
choices = append(choices, root)
|
|
backtrackIII(&state, &choices, &res)
|
|
|
|
fmt.Println("\nOutput all paths from root node to node 7, paths do not include nodes with value 3")
|
|
for _, path := range res {
|
|
for _, node := range path {
|
|
fmt.Printf("%v ", node.Val)
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|