145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes’ values.
Example:
Input: [1,null,2,3]
1
2
/
3
Output: [3,2,1]
递归吧,简单
func postorderTraversal(root *TreeNode) []int {
if root == nil {
return []int{}
}
res := []int{}
postOrder(root, &res)
return res
}
func postOrder(root *TreeNode, ans *[]int) {
if root == nil {
return
}
postOrder(root.Left, ans)
postOrder(root.Right, ans)
*ans = append(*ans, root.Val)
}