Go solution. Its a bit verbose as it does not have optional/default params.
package main import ( "fmt" "strings" ) type Node struct { val string left *Node right *Node } func (node *Node) serialize() string { if node == nil { return "#" } leftSide := node.left.serialize() rightSide := node.right.serialize() ret := fmt.Sprintf("%s->%s->%s", node.val, leftSide, rightSide) return ret } func deserialize(str string) Node { node, _ := deserailizeNodes(strings.Split(str, "->")) return node } func deserailizeNodes(nodes []string) (Node, []string) { if len(nodes) == 0 { return Node{"", nil, nil}, nil } nextNode := nodes[0] if nextNode == "#" { return Node{"", nil, nil}, nodes[1:] } left, rem := deserailizeNodes(nodes[1:]) right, _ := deserailizeNodes(rem) if left.val == "" && right.val == "" { return Node{nextNode, nil, nil}, nil } if left.val == "" { return Node{nextNode, nil, &right}, nil } if right.val == "" { return Node{nextNode, &left, nil}, nil } node := Node{val: nextNode, left: &left, right: &right} return node, nil } func main() { node := Node{"root", &Node{"left", &Node{"left.left", nil, nil}, nil}, &Node{"right", nil, nil}} fmt.Println(node.serialize()) // root->left->left.left->#->#->#->right->#-># n := deserialize(node.serialize()) fmt.Println(n.left.left.val == "left.left") // true }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a blogging-forward open source social network where we learn from one another
Go solution. Its a bit verbose as it does not have optional/default params.