94. Binary Tree Inorder Traversal.go

二叉树中序遍历,递归

func inorderTraversal(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)
    }
    *res = append(*res, root.Val)
    if root.Right != nil {
        helper(res, root.Right)
    }
}

你可能感兴趣的:(94. Binary Tree Inorder Traversal.go)