Beispiel #1
0
        public static TreeNode AddToTree()
        {
            if (i >= global.Length - 1)
            {
                return null;
            }

            if (global[i] == 'n' || global[i] == 't')
            {
                i++;
                return null;
            }

            string k = "";

            while (global[i]!='t' && global[i] != 'n')
            {
                k = k + global[i];
                i++;
            }

            head = new TreeNode(int.Parse(k));
            i++;
            head.left = AddToTree();
            head.right = AddToTree();
            return head;
        }
Beispiel #2
0
 public static void Rec(TreeNode root)
 {
     if (root == null)
     {
         sklad.Add("n");
         return;
     }
     sklad.Add("t");
     sklad.Add(root.val.ToString());
     Rec(root.left);
     Rec(root.right);
 }
Beispiel #3
0
        public static string serialize(TreeNode root)
        {
            if (root == null)
            {
                return "";
            }
            sklad = new List<string>();
            Rec(root);
            StringBuilder builder = new StringBuilder();

            for (int i = 1; i < sklad.Count; i++)
            {
                builder.Append(sklad[i]);
            }
            return builder.ToString();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            TreeNode tN = new TreeNode(1);
            tN.right = new TreeNode(3);
            tN.left = new TreeNode(2);
            tN.right.right = new TreeNode(5);
            tN.right.left = new TreeNode(4);

            string k = serialize(tN);
            deserialize(k);
        }