Deserializes your encoded data to tree.
(data string)
| 41 | |
| 42 | // Deserializes your encoded data to tree. |
| 43 | func (this *Codec) deserialize(data string) *TreeNode { |
| 44 | index := -1 |
| 45 | arr := strings.Split(data, ",") |
| 46 | var dfs func() *TreeNode |
| 47 | dfs = func() *TreeNode { |
| 48 | index++ |
| 49 | if index >= len(data) || arr[index] == "x" { |
| 50 | |
| 51 | return nil |
| 52 | } |
| 53 | val, _ := strconv.Atoi(arr[index]) |
| 54 | |
| 55 | head := &TreeNode{Val: val} |
| 56 | head.Left = dfs() |
| 57 | head.Right = dfs() |
| 58 | return head |
| 59 | } |
| 60 | return dfs() |
| 61 | } |