题解 | #重建二叉树#
重建二叉树
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
}
查看14道真题和解析
联想公司福利 1481人发布