W przedsprzedaży Traversal Golang
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func preOrderTraverse(root *TreeNode) int[] {
list := []int {}
if root == nil {
return list
}
stack := []*TreeNode { root }
for len(stack) > 0 {
root = stack[len(stack) - 1]
stack = stack[0:len(stack) - 1]
list = append(list, root.Val)
if root.Left != nil {
stack = append(stack, root.Left)
}
if root.Right != nil {
stack = append(stack, root.Right)
}
}
return list
}
Ibrahim Albarghouthi