diff --git a/main.go b/main.go deleted file mode 100644 index c1a2920..0000000 --- a/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "log" - -func main() { - log.Println(problem2([]int{3, 2, 1})) -} diff --git a/problem3.go b/problem3.go new file mode 100644 index 0000000..03247b7 --- /dev/null +++ b/problem3.go @@ -0,0 +1,80 @@ +package main + +import ( + "log" + "strings" +) + +//Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. +// +//For example, given the following Node class +// +//class Node: +// def __init__(self, val, left=None, right=None): +// self.val = val +// self.left = left +// self.right = right +//The following test should pass: +// +//node = Node('root', Node('left', Node('left.left')), Node('right')) +//assert deserialize(serialize(node)).left.left.val == 'left.left' + +type Node struct { + Val string + Left *Node + Right *Node +} + +func (n *Node) Serialize() string { + if *n == (Node{}) { + return "" + } + return n.Val + "\u200b" + n.Left.Serialize() + "\u200b" + n.Right.Serialize() + "\u200b" +} + +func Deserialize(s string) *Node { + sc := make(chan string) + go func() { + for _, val := range strings.Split(s, "\u200b") { + sc <- val + } + close(sc) + }() + return processNode(sc) +} + +func processNode(sc <-chan string) *Node { + root := Node{} + for val := range sc { + if val == "" { + return &root + } + root.Val = val + root.Left = processNode(sc) + root.Right = processNode(sc) + } + return &root +} + +func main() { + n := &Node{ + Val: "root", + Left: &Node{ + Val: "left", + Left: &Node{ + Val: "left.left", + Left: &Node{}, + Right: &Node{}, + }, + Right: &Node{}, + }, + Right: &Node{ + Val: "right", + Left: &Node{}, + Right: &Node{}, + }, + } + s := n.Serialize() + log.Println(s) + log.Println(Deserialize(s).Left.Left.Val) +}