mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-02 03:05:24 +08:00
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
45 lines
875 B
Go
45 lines
875 B
Go
// File: binary_tree_dfs.go
|
|
// Created Time: 2022-11-26
|
|
// Author: Reanon ([email protected])
|
|
|
|
package chapter_tree
|
|
|
|
import (
|
|
. "github.com/krahets/hello-algo/pkg"
|
|
)
|
|
|
|
var nums []any
|
|
|
|
/* 行きがけ順走査 */
|
|
func preOrder(node *TreeNode) {
|
|
if node == nil {
|
|
return
|
|
}
|
|
// 訪問順序:根ノード -> 左部分木 -> 右部分木
|
|
nums = append(nums, node.Val)
|
|
preOrder(node.Left)
|
|
preOrder(node.Right)
|
|
}
|
|
|
|
/* 通りがけ順走査 */
|
|
func inOrder(node *TreeNode) {
|
|
if node == nil {
|
|
return
|
|
}
|
|
// 訪問順序:左部分木 -> 根ノード -> 右部分木
|
|
inOrder(node.Left)
|
|
nums = append(nums, node.Val)
|
|
inOrder(node.Right)
|
|
}
|
|
|
|
/* 帰りがけ順走査 */
|
|
func postOrder(node *TreeNode) {
|
|
if node == nil {
|
|
return
|
|
}
|
|
// 訪問順序:左部分木 -> 右部分木 -> 根ノード
|
|
postOrder(node.Left)
|
|
postOrder(node.Right)
|
|
nums = append(nums, node.Val)
|
|
}
|