0
0
DSA Javascriptprogramming~10 mins

Serialize and Deserialize Binary Tree in DSA Javascript - 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 Javascript
function TreeNode(val) {
  this.val = [1];
  this.left = null;
  this.right = null;
}
Drag options to blanks, or click blank then click option'
Aval
Bvalue
Cdata
Dnode
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the parameter.
Assigning undefined or null instead of the parameter.
2fill in blank
medium

Complete the code to add a node's value to the result array during serialization.

DSA Javascript
function serialize(root) {
  const result = [];
  function dfs(node) {
    if (!node) {
      result.push('null');
      return;
    }
    result.push(node[1]);
    dfs(node.left);
    dfs(node.right);
  }
  dfs(root);
  return result.join(',');
}
Drag options to blanks, or click blank then click option'
A.node
B.value
C.val
D.data
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like 'value' or 'data'.
Trying to push the whole node instead of its value.
3fill in blank
hard

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

DSA Javascript
function deserialize(data) {
  const values = data.split(',');
  function dfs() {
    if (values[0] === 'null') {
      values.shift();
      return null;
    }
    const node = new TreeNode(parseInt(values[1]));
    node.left = dfs();
    node.right = dfs();
    return node;
  }
  return dfs();
}
Drag options to blanks, or click blank then click option'
A.shift()
B[1]
C[0]
D.pop()
Attempts:
3 left
💡 Hint
Common Mistakes
Using indexing without removing the element causes infinite loops.
Using pop removes the last element, which is incorrect here.
4fill in blank
hard

Fill both blanks to correctly serialize a binary tree using preorder traversal.

DSA Javascript
function serialize(root) {
  const result = [];
  function dfs(node) {
    if (node === null) {
      result.push([1]);
      return;
    }
    result.push(node[2]);
    dfs(node.left);
    dfs(node.right);
  }
  dfs(root);
  return result.join(',');
}
Drag options to blanks, or click blank then click option'
A'null'
Bnull
C.val
D.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using null (without quotes) instead of 'null' string.
Using incorrect property names like 'value'.
5fill in blank
hard

Fill all three blanks to correctly deserialize a binary tree from a string.

DSA Javascript
function deserialize(data) {
  const values = data.split(',');
  function dfs() {
    if (values[1] === [2]) {
      values.shift();
      return [3];
    }
    const node = new TreeNode(parseInt(values.shift()));
    node.left = dfs();
    node.right = dfs();
    return node;
  }
  return dfs();
}
Drag options to blanks, or click blank then click option'
A[0]
B'null'
Cnull
D[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to null instead of 'null' string.
Using wrong index or removing elements incorrectly.