func postorderTraversal(root *TreeNode) []int {
res := []int{}
if root == nil {
return res
}
stack := []*TreeNode{}
stack = append(stack, root)
for len(stack) != 0 {
node := stack[len(stack)-1]
stack = stack[:len(stack)-1]
res = append(res, node.Val)
if node.Left != nil {
stack = append(stack, node.Left)
}
if node.Right != nil {
stack = append(stack, node.Right)
}
}
reverse(&res)
return res
}
func reverse(arr *[]int) {
i, j := 0, len(*arr)-1
for i < j {
(*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]
i++
j--
}
}