Leetcode Golang 145. Binary Tree Postorder Traversal.go

2019-04-12 10:42:45 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412856

思路

后序遍历

code

代码语言:javascript复制
func postorderTraversal(root *TreeNode) []int {
	res := []int{}
	if root == nil {
		return res
	}
	helper(&res, root)
	return res
}

func helper(res *[]int, root *TreeNode) {
	if root.Left != nil {
		helper(res, root.Left)
	}
	if root.Right != nil {
		helper(res, root.Right)
	}
	*res = append(*res, root.Val)
}

0 人点赞