Leetcodeleetcode

94. Binary Tree Inorder Traversa

2019-03-24  本文已影响4人  AnakinSun

二叉树中序遍历,递归

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)
    }
}
上一篇 下一篇

猜你喜欢

热点阅读