题目:
题解:
type Codec struct{}
func Constructor() (_ Codec) { return }
func (Codec) serialize(root *TreeNode) string {
arr := []string{}
var postOrder func(*TreeNode)
postOrder = func(node *TreeNode) {
if node == nil {
return
}
postOrder(node.Left)
postOrder(node.Right)
arr = append(arr, strconv.Itoa(node.Val))
}
postOrder(root)
return strings.Join(arr, " ")
}
func (Codec) deserialize(data string) *TreeNode {
if data == "" {
return nil
}
arr := strings.Split(data, " ")
var construct func(int, int) *TreeNode
construct = func(lower, upper int) *TreeNode {
if len(arr) == 0 {
return nil
}
val, _ := strconv.Atoi(arr[len(arr)-1])
if val < lower || val > upper {
return nil
}
arr = arr[:len(arr)-1]
return &TreeNode{Val: val, Right: construct(val, upper), Left: construct(lower, val)}
}
return construct(math.MinInt32, math.MaxInt32)
}