美文网首页Leetcodeleetcode
94. Binary Tree Inorder Traversa

94. Binary Tree Inorder Traversa

作者: AnakinSun | 来源:发表于2019-03-24 20:59 被阅读4次

二叉树中序遍历,递归

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 Traversa

    本文链接:https://www.haomeiwen.com/subject/snjpvqtx.html