0
0
DSA Typescriptprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Typescript - Interactive Practice

Choose your learning style9 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.