From 48f683e7cfce9c12aa95fb70034b6c14a3dcdf87 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Thu, 7 Jan 2021 21:30:16 -0700 Subject: [PATCH] added solution and test cases for 449 --- .../java/com/fishercoder/solutions/_449.java | 40 +++++++++++++++++++ src/test/java/com/fishercoder/_449Test.java | 4 ++ 2 files changed, 44 insertions(+) diff --git a/src/main/java/com/fishercoder/solutions/_449.java b/src/main/java/com/fishercoder/solutions/_449.java index 057c875b33..7ff4cbb993 100644 --- a/src/main/java/com/fishercoder/solutions/_449.java +++ b/src/main/java/com/fishercoder/solutions/_449.java @@ -2,6 +2,7 @@ import com.fishercoder.common.classes.TreeNode; +import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; @@ -150,4 +151,43 @@ public TreeNode deserialize(String data) { return root; } } + public static class Solution4 { + private static final String NULL_SYMBOL = "X"; + private static final String DELIMITER = ","; + + // Encodes a tree to a single string. + public String serialize(TreeNode root) { + + // If we have a null symbol, encode it to NULL_SYMBOL + if(root == null) + return NULL_SYMBOL + DELIMITER; + + String leftSubtree = serialize(root.left); + String rightSubtree = serialize(root.right); + + return root.val + DELIMITER + leftSubtree + rightSubtree; + } + + // Decodes your encoded data to tree. + public TreeNode deserialize(String data) { + + Queue nodesLeftToSerialize = new LinkedList<>(); + nodesLeftToSerialize.addAll(Arrays.asList(data.split(DELIMITER))); + return deserializeHelper(nodesLeftToSerialize); + + } + private TreeNode deserializeHelper(Queue nodesLeft){ + + // remove the node + String nodeLeftToSerialize = nodesLeft.poll(); + // base case + if(nodeLeftToSerialize.equals(NULL_SYMBOL)){ + return null; + } + TreeNode newNode = new TreeNode(Integer.valueOf(nodeLeftToSerialize)); + newNode.left = deserializeHelper(nodesLeft); + newNode.right = deserializeHelper(nodesLeft); + return newNode; + } + } } diff --git a/src/test/java/com/fishercoder/_449Test.java b/src/test/java/com/fishercoder/_449Test.java index 96ea8a499f..a07e9bcfa6 100644 --- a/src/test/java/com/fishercoder/_449Test.java +++ b/src/test/java/com/fishercoder/_449Test.java @@ -12,6 +12,7 @@ public class _449Test { private static _449.Solution1 solution1; private static _449.Solution2 solution2; private static _449.Solution3 solution3; + private static _449.Solution4 solution4; private static TreeNode expectedRoot; @BeforeClass @@ -19,6 +20,7 @@ public static void setup() { solution1 = new _449.Solution1(); solution2 = new _449.Solution2(); solution3 = new _449.Solution3(); + solution4 = new _449.Solution4(); } @Before @@ -34,6 +36,7 @@ public void test1() { assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString()); assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString()); assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString()); + assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString()); } @Test @@ -44,5 +47,6 @@ public void test2() { assertEquals(expectedRoot.toString(), solution1.deserialize(solution1.serialize(expectedRoot)).toString()); assertEquals(expectedRoot.toString(), solution2.deserialize(solution2.serialize(expectedRoot)).toString()); assertEquals(expectedRoot.toString(), solution3.deserialize(solution3.serialize(expectedRoot)).toString()); + assertEquals(expectedRoot.toString(), solution4.deserialize(solution4.serialize(expectedRoot)).toString()); } }