0
0
DSA Typescriptprogramming~20 mins

Serialize and Deserialize Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Serialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Preorder Serialization
What is the output of the following TypeScript code that serializes a binary tree using preorder traversal with 'null' for empty nodes?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function serialize(root: TreeNode | null): string {
  if (!root) return 'null';
  return root.val + ',' + serialize(root.left) + ',' + serialize(root.right);
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)));
console.log(serialize(tree));
A"1,2,null,3,4,null,null,5,null,null"
B"1,2,null,null,3,4,5,null,null,null"
C"1,2,null,null,3,4,null,null,5,null,null"
D"1,2,3,4,5,null,null,null,null,null,null"
Attempts:
2 left
💡 Hint
Think about preorder traversal: root, left subtree, then right subtree. Use 'null' for missing children.
Predict Output
intermediate
2:00remaining
Result of Deserializing a Serialized Tree
Given the serialized string "1,2,null,null,3,4,null,null,5,null,null", what is the value of the root's right child's left child after deserializing with the code below?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function deserialize(data: string): TreeNode | null {
  const list = data.split(',');
  function helper(): TreeNode | null {
    if (list.length === 0) return null;
    const val = list.shift();
    if (val === 'null') return null;
    const node = new TreeNode(Number(val));
    node.left = helper();
    node.right = helper();
    return node;
  }
  return helper();
}

const root = deserialize("1,2,null,null,3,4,null,null,5,null,null");
console.log(root?.right?.left?.val);
A4
B5
C3
Dnull
Attempts:
2 left
💡 Hint
Trace the deserialization step by step, matching nodes to values in preorder.
🔧 Debug
advanced
2:00remaining
Identify the Error in Serialization Code
What error does the following TypeScript serialization code produce when run on a non-empty tree?
DSA Typescript
function serialize(root: TreeNode | null): string {
  if (!root) return '';
  return root.val + ',' + serialize(root.left) + ',' + serialize(root.right);
}

const tree = new TreeNode(1, new TreeNode(2), null);
console.log(serialize(tree));
ASyntaxError due to missing return type
BThe output string has extra commas and missing 'null' for empty nodes
CTypeError because root.val is undefined
DRuntime error due to infinite recursion
Attempts:
2 left
💡 Hint
Consider what happens when a child node is null and how it is represented in the output string.
🚀 Application
advanced
2:00remaining
Number of Nodes After Deserialization
After deserializing the string "1,2,null,null,3,null,4,null,null" using the standard preorder deserialization method, how many nodes does the resulting binary tree contain?
DSA Typescript
function deserialize(data: string): TreeNode | null {
  const list = data.split(',');
  function helper(): TreeNode | null {
    if (list.length === 0) return null;
    const val = list.shift();
    if (val === 'null') return null;
    const node = new TreeNode(Number(val));
    node.left = helper();
    node.right = helper();
    return node;
  }
  return helper();
}

const root = deserialize("1,2,null,null,3,null,4,null,null");
A5
B3
C6
D4
Attempts:
2 left
💡 Hint
Count nodes by tracing the preorder deserialization and ignoring 'null' markers.
🧠 Conceptual
expert
2:00remaining
Why Use 'null' Markers in Serialization?
Why is it important to include explicit 'null' markers for empty nodes when serializing a binary tree using preorder traversal?
ATo preserve the exact tree structure including missing children during deserialization
BTo reduce the size of the serialized string by skipping empty nodes
CTo make the serialization compatible with inorder traversal
DTo allow the tree to be serialized without recursion
Attempts:
2 left
💡 Hint
Think about what happens if you omit empty nodes in the serialized data.