Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/1f88954d31d1ea7c18a2e5f17ed4ab97 to your computer and use it in GitHub Desktop.
Save jianminchen/1f88954d31d1ea7c18a2e5f17ed4ab97 to your computer and use it in GitHub Desktop.
Leetcode 297 - Serialize and Deserialize tree - use post order traversal (?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _297SerializeDeserializeTree_Stack
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
}
/*
* May 23, 2016
* Preorder traversal - Spend time to understand the idea later.
*
*/
public static string serialize(TreeNode root)
{
// nodes will be added to an arrayList of arrays; then, convert the arraylist to a string.
string emptyString = "";
if (root == null) return emptyString;
List<int[]> list = new List<int[]>();
Stack<TreeNode> stack = new Stack<TreeNode>();
Stack<Int32> intStack = new Stack<Int32>();
TreeNode cur = root;
int ci = 0;
while (true)
{ // preorder traversal to construct the arraylist ? should be post order ?
while (cur != null)
{
if (stack.Count > 0)
{ // get the parent, and set the left and right of the parent.
int pi = intStack.Peek(); // ci is the current index
if (cur == stack.Peek().left)
{ // cur is left child of the parent
list[pi][1] = ci;
}
else
{ // cur is the right child of the parent
list[pi][2] = ci;
}
}
list.Add(new int[] { cur.val, -1, -1 });
stack.Push(cur);
intStack.Push(ci);
cur = cur.left;
++ci;
}
while (stack.Count > 0 && (stack.Peek().right == null || stack.Peek().right == cur))
{
cur = stack.Pop();
intStack.Pop();
}
if (stack.Count == 0 )
break;
else
cur = stack.Peek().right;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; ++i)
{
for (int j = 0; j < 3; ++j)
{
sb.Append(list[i][j]);
sb.Append(' ');
}
}
return sb.ToString();
}
// Decodes your encoded data to tree.
public TreeNode deserialize(string data)
{
// convert the string into an arrayList; then, reconstruct the tree using the arraylist.
if (data.Length == 0)
return null;
List<Int32> nodes = new List<Int32>();
int count = 0;
while (count < data.Length)
{
int si = count;
while (data[count] != ' ')
++count;
int ei = count;
nodes.Add(Int32.Parse(data.Substring(si, ei-si+1)));
++count;
}
List<int[]> list = new List<int[]>();
for (int i = 0; i < nodes.Count / 3; ++i)
{
int[] node = new int[3];
for (int j = 0; j < 3; ++j)
{
node[j] = nodes[i * 3 + j];
}
list.Add(node);
}
// reconstruct the tree using the list of arrays.
TreeNode root = generateTree(list, 0);
return root;
}
private TreeNode generateTree(List<int[]> list, int index)
{
int[] arr = new int[3] { list[index][0], list[index][1], list[index][2] };
TreeNode node = new TreeNode(arr[0]);
node.left = arr[1] == -1 ? null : generateTree(list, arr[1]);
node.right = arr[2] == -1 ? null : generateTree(list, arr[2]);
return node;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment