leet-code/binary-tree-postorder-trave.../sol.go

31 lines
560 B
Go
Raw Permalink Normal View History

2022-06-09 14:45:14 +00:00
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func postorderTraversalInPlace(root *TreeNode, list *[]int) {
if root == nil {
return
}
postorderTraversalInPlace(root.Left, list)
postorderTraversalInPlace(root.Right, list)
*list = append(*list, root.Val)
}
func postorderTraversal(root *TreeNode) []int {
list := &[]int{}
postorderTraversalInPlace(root, list)
return *list
}