What is Nostr?
dave /
npub1tsg…htk4
2023-02-08 05:09:23
in reply to nevent1q…x0mu

dave on Nostr: // 前序遍历 func PreorderTraversal(root *TreeNode) []int { res := []int{} ...

// 前序遍历
func PreorderTraversal(root *TreeNode) []int {
res := []int{}
rootNode := root
stack := []*TreeNode{}
for rootNode != nil || len(stack) > 0 {
for rootNode != nil {
res = append(res, rootNode.Val)
stack = append(stack, rootNode)
rootNode = rootNode.Left
}
if len(stack) > 0 {
rootNode = stack[len(stack)-1]
stack = stack[:len(stack)-1]
rootNode = rootNode.Right
}
}
return res
}

// 中序遍历
func InorderTraversal(root *TreeNode) []int {
res := []int{}
stack := []*TreeNode{}
rootNode := root
for rootNode != nil || len(stack) > 0 {
for rootNode != nil {
stack = append(stack, rootNode)
rootNode = rootNode.Left
}
if len(stack) > 0 {
rootNode = stack[len(stack)-1]
stack = stack[:len(stack)-1]
res = append(res, rootNode.Val)
rootNode = rootNode.Right
}
}
return res
}

// 后序遍历
func PostorderTraversal(root *TreeNode) []int {
res := []int{}
rootNode := root
stack := []*TreeNode{}
for rootNode != nil || len(stack) > 0 {
for rootNode != nil {
stack = append(stack, rootNode)
res = append([]int{rootNode.Val}, res...)
rootNode = rootNode.Right
}
if len(stack) > 0 {
rootNode = stack[len(stack)-1]
stack = stack[:len(
Author Public Key
npub1tsgw6pncspg4d5u778hk63s3pls70evs4czfsmx0fzap9xwt203qtkhtk4