题解 | #序列化二叉树#一时递归一时爽,一直递归一直爽
序列化二叉树
https://www.nowcoder.com/practice/cf7e25aa97c04cc1a68c8f040e71fb84
package main import ( "strconv" "strings" ) type TreeNode struct { Val int Left, Right *TreeNode } func Serialize(root *TreeNode) string { // write code here if root == nil { return "#" } return strconv.Itoa(root.Val) + "," + Serialize(root.Left) + "," + Serialize(root.Right) } func Deserialize(s string) *TreeNode { // write code here list := strings.Split(s, ",") var travc func()*TreeNode travc=func() *TreeNode { val:=list[0] list=list[1:] if val=="#"{ return nil } value,_:=strconv.Atoi(val) return &TreeNode{value,travc(),travc()} } return travc() }一时递归一时爽,一直递归一直爽