题目:
题解:
type Codec struct{}func Constructor() (_ Codec) {return
}func (c Codec) serialize(root *TreeNode) string {if root == nil {return "X"}left := "(" + c.serialize(root.Left) + ")"right := "(" + c.serialize(root.Right) + ")"return left + strconv.Itoa(root.Val) + right
}func (Codec) deserialize(data string) *TreeNode {var parse func() *TreeNodeparse = func() *TreeNode {if data[0] == 'X' {data = data[1:]return nil}node := &TreeNode{}data = data[1:] // 跳过左括号node.Left = parse()data = data[1:] // 跳过右括号i := 0for data[i] == '-' || '0' <= data[i] && data[i] <= '9' {i++}node.Val, _ = strconv.Atoi(data[:i])data = data[i:]data = data[1:] // 跳过左括号node.Right = parse()data = data[1:] // 跳过右括号return node}return parse()
}