-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
72 lines (66 loc) · 1.52 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SerializeAndDeserializeBinaryTree
{
class Program
{
static void Main(string[] args)
{
var nodes = "1,null,2,3,null,null,null";
var root = Build(nodes.Split(","));
var codec = new Codec();
var data = codec.serialize(root);
Console.WriteLine(data);
Console.WriteLine(codec.deserialize(data));
}
static int index = 0;
private static TreeNode Build(string[] nodes)
{
var first = nodes[index++];
if (first == "null") return null;
var root = new TreeNode(int.Parse(first)) {left = Build(nodes), right = Build(nodes)};
return root;
}
}
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
public class Codec
{
private int index;
public string serialize(TreeNode root)
{
var preOrder = new StringBuilder();
PreOrder(root, preOrder);
return preOrder.ToString();
}
public TreeNode deserialize(string data)
{
return Build(data.Split(","));
}
private void PreOrder(TreeNode root, StringBuilder nodes)
{
if (root == null)
{
nodes.Append("#,");
return;
}
nodes.Append($"{root.val.ToString()},");
PreOrder(root.left, nodes);
PreOrder(root.right, nodes);
}
private TreeNode Build(string[] data)
{
string first = data[index++];
if (first == "#") return null;
var root = new TreeNode(int.Parse(first)) { left = Build(data), right = Build(data) };
return root;
}
}
}