题解 | #重建二叉树#

重建二叉树

http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6

package main
import . "nc_tools"

//递归,时空On
func reConstructBinaryTree( pre []int ,  vin []int ) *TreeNode {
    if len(pre) == 0 {
        return nil
    }

    root := &TreeNode{pre[0], nil, nil}

    i := 0
    for i = 0; i < len(vin); i++ {
        if vin[i] == pre[0] {
            break
        }
    }

    root.Left = reConstructBinaryTree(pre[1: len(vin[: i]) +1], vin[: i])
    root.Right = reConstructBinaryTree(pre[len(vin[: i]) +1: ], vin[i +1:])

    return root
}
全部评论
len(vin[: i]) +1 不就是i+1吗,为什么i+1不对
1 回复 分享
发布于 2022-05-21 15:35

相关推荐

饼子吃到撑:当我看到外企的时候,我就知道这大概率可能是真的
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务