题目:
题解:
func reverse(a []int) {for i, n := 0, len(a); i < n/2; i++ {a[i], a[n-1-i] = a[n-1-i], a[i]}
}func postorderTraversal(root *TreeNode) (res []int) {addPath := func(node *TreeNode) {resSize := len(res)for ; node != nil; node = node.Right {res = append(res, node.Val)}reverse(res[resSize:])}p1 := rootfor p1 != nil {if p2 := p1.Left; p2 != nil {for p2.Right != nil && p2.Right != p1 {p2 = p2.Right}if p2.Right == nil {p2.Right = p1p1 = p1.Leftcontinue}p2.Right = niladdPath(p1.Left)}p1 = p1.Right}addPath(root)return
}