Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the given value.

DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val: number) {
    this.val = [1];
    this.left = null;
    this.right = null;
  }
}
Drag options to blanks, or click blank then click option'
Avalue
Bval
Cnode
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not defined in the constructor.
Assigning 'this.val' to a wrong variable.
2fill in blank
medium

Complete the code to serialize the binary tree using preorder traversal.

DSA Typescript
function serialize(root: TreeNode | null): string {
  const result: string[] = [];
  function dfs(node: TreeNode | null) {
    if (node === null) {
      result.push('null');
      return;
    }
    result.push(node.val.toString());
    dfs(node.[1]);
    dfs(node.right);
  }
  dfs(root);
  return result.join(',');
}
Drag options to blanks, or click blank then click option'
Aval
Broot
Cright
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Calling dfs on the right child before the left child.
Using an incorrect property name.
3fill in blank
hard

Fix the error in the deserialize function to correctly parse the next node value.

DSA Typescript
function deserialize(data: string): TreeNode | null {
  const values = data.split(',');
  let index = 0;
  function dfs(): TreeNode | null {
    if (values[[1]] === 'null') {
      index++;
      return null;
    }
    const node = new TreeNode(parseInt(values[index]));
    index++;
    node.left = dfs();
    node.right = dfs();
    return node;
  }
  return dfs();
}
Drag options to blanks, or click blank then click option'
A1
B0
Cindex
Dvalues.length
Attempts:
3 left
💡 Hint
Common Mistakes
Checking a fixed index like 0 or 1 instead of the current index.
Using values.length which is out of bounds.
4fill in blank
hard

Fill in the blank to correctly implement the base case in deserialize.

DSA Typescript
function deserialize(data: string): TreeNode | null {
  const values = data.split(',');
  let index = 0;
  function dfs(): TreeNode | null {
    if (values[index] [1] 'null') {
      index++;
      return null;
    }
    const node = new TreeNode(parseInt(values[index]));
    index++;
    node.left = dfs();
    node.right = dfs();
    return node;
  }
  return dfs();
}
Drag options to blanks, or click blank then click option'
A===
B!==
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' or '!==', which would invert the condition.
Using comparison operators like '<' or '>'.
5fill in blank
hard

Fill all three blanks to complete the serialize function using preorder traversal with null markers.

DSA Typescript
function serialize(root: TreeNode | null): string {
  const result: string[] = [];
  function dfs(node: TreeNode | null) {
    if (node === [1]) {
      result.push([2]);
      return;
    }
    result.push(node.val.toString());
    dfs(node.left);
    dfs(node.[3]);
  }
  dfs(root);
  return result.join(',');
}
Drag options to blanks, or click blank then click option'
Anull
B'null'
Cright
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for undefined instead of null.
Pushing null instead of the string 'null'.
Recursing on the wrong child.

Practice

(1/5)
1. What is the main purpose of serialize and deserialize functions in the context of a binary tree?
easy
A. Find the height of the binary tree
B. Sort the binary tree nodes in ascending order
C. Convert a binary tree to a string and back to a binary tree
D. Delete all nodes in the binary tree

Solution

  1. Step 1: Understand serialization

    Serialization means converting a binary tree into a string format that can be stored or sent.
  2. Step 2: Understand deserialization

    Deserialization means converting the string back into the original binary tree structure.
  3. Final Answer:

    Convert a binary tree to a string and back to a binary tree -> Option C
  4. Quick Check:

    Serialize + Deserialize = Convert tree to string and back [OK]
Hint: Serialization means tree to string, deserialization reverses it [OK]
Common Mistakes:
  • Confusing serialization with sorting
  • Thinking deserialization deletes nodes
  • Mixing serialization with tree height calculation
2. Which of the following TypeScript function signatures correctly represents a deserialize function for a binary tree?
easy
A. function deserialize(data: string): TreeNode | null
B. function deserialize(data: number): TreeNode
C. function deserialize(tree: TreeNode): string
D. function deserialize(): void

Solution

  1. Step 1: Identify input and output types

    Deserialize takes a string (serialized data) and returns a TreeNode or null if empty.
  2. Step 2: Match correct signature

    function deserialize(data: string): TreeNode | null matches input as string and output as TreeNode or null.
  3. Final Answer:

    function deserialize(data: string): TreeNode | null -> Option A
  4. Quick Check:

    Deserialize input string, output tree node [OK]
Hint: Deserialize input is string, output is tree node or null [OK]
Common Mistakes:
  • Using number instead of string as input
  • Returning string instead of tree node
  • No input or void return type
3. Given the following serialized string of a binary tree using preorder with '#' as null marker: "1,2,#,#,3,4,#,#,5,#,#", what is the inorder traversal of the deserialized tree?
medium
A. [1, 2, 3, 4, 5]
B. [2, 1, 4, 3, 5]
C. [2, 4, 1, 3, 5]
D. [1, 3, 2, 4, 5]

Solution

  1. Step 1: Reconstruct tree from preorder string

    Preorder: 1 (root), 2 (left), #,# (2's children null), 3 (right), 4 (3's left), #,# (4's children null), 5 (3's right), #,# (5's children null).
  2. Step 2: Perform inorder traversal (left, root, right)

    Inorder: left subtree of 1 is 2, root 1, left subtree of 3 is 4, root 3, right subtree 5. So inorder is [2, 1, 4, 3, 5].
  3. Final Answer:

    [2, 1, 4, 3, 5] -> Option B
  4. Quick Check:

    Preorder string to tree, inorder traversal = [2,1,4,3,5] [OK]
Hint: Rebuild tree from preorder, then do inorder traversal [OK]
Common Mistakes:
  • Confusing preorder with inorder
  • Misplacing null markers
  • Incorrect traversal order
4. Consider this deserialize function snippet in TypeScript:
function deserialize(data: string): TreeNode | null {
  const values = data.split(",");
  function build(): TreeNode | null {
    if (!values.length) return null;
    const val = values.shift();
    if (val === "#") return null;
    const node = new TreeNode(parseInt(val));
    node.left = build();
    node.right = build();
    return node;
  }
  return build();
}
What is the bug in this code if the input string is empty?
medium
A. It returns null correctly, no bug
B. parseInt throws error on undefined
C. The function build never terminates
D. parseInt('') returns NaN, creating invalid TreeNode

Solution

  1. Step 1: Check behavior on empty input

    If data is empty string, values = [""], so values.length is 1, not zero.
  2. Step 2: Analyze values.shift() and val check

    values.shift() returns "" (empty string), val === "#" is false, parseInt("") returns NaN, which may cause issues when creating TreeNode.
  3. Step 3: Identify actual bug

    parseInt("") returns NaN, so TreeNode is created with NaN value, which is likely unintended.
  4. Final Answer:

    parseInt('') returns NaN, creating invalid TreeNode -> Option D
  5. Quick Check:

    Empty input leads to undefined or NaN node value [OK]
Hint: Empty string splits to [''], not empty array; check val before parseInt [OK]
Common Mistakes:
  • Assuming empty string splits to empty array
  • Ignoring parseInt('') returns NaN
  • Thinking function never ends
5. You want to serialize a binary tree using level-order traversal (BFS) with 'null' for missing nodes. Which of these serialized strings correctly represents the tree:

Tree:
1
/ \ 2 3
/ \ 4 5

Options:
hard
A. "1,2,3,null,null,4,5,null,null,null,null"
B. "1,2,3,4,5,null,null,null,null,null,null"
C. "1,3,2,null,4,5,null,null,null,null,null"
D. "1,2,3,4,null,5,null,null,null,null,null"

Solution

  1. Step 1: Understand level-order traversal

    Level order visits nodes level by level: root, then children left to right, including nulls for missing nodes.
  2. Step 2: Write level order for given tree

    Level 1: 1
    Level 2: 2, 3
    Level 3: null, null (2's children), 4, 5 (3's children)
    Level 4: nulls for 4 and 5's children
    So serialized string is "1,2,3,null,null,4,5,null,null,null,null".
  3. Final Answer:

    "1,2,3,null,null,4,5,null,null,null,null" -> Option A
  4. Quick Check:

    Level order with nulls matches "1,2,3,null,null,4,5,null,null,null,null" [OK]
Hint: Level order includes nulls for missing children [OK]
Common Mistakes:
  • Mixing preorder with level order
  • Omitting null placeholders
  • Swapping left and right children
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard