0
0
DSA Javascriptprogramming~10 mins

Tree Traversal Inorder Left Root Right 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 visit the left subtree first in inorder traversal.

DSA Javascript
function inorder(node) {
  if (node === null) return;
  inorder(node.[1]);
  console.log(node.value);
  inorder(node.right);
}
Drag options to blanks, or click blank then click option'
Aparent
Bright
Croot
Dleft
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' causes wrong traversal order.
Using 'root' or 'parent' properties which do not exist.
2fill in blank
medium

Complete the code to print the current node's value in inorder traversal.

DSA Javascript
function inorder(node) {
  if (node === null) return;
  inorder(node.left);
  console.log(node.[1]);
  inorder(node.right);
}
Drag options to blanks, or click blank then click option'
Avalue
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'left' or 'right' instead of the node's value.
Trying to print 'parent' which is not defined.
3fill in blank
hard

Fix the error in the inorder traversal to visit the right subtree correctly.

DSA Javascript
function inorder(node) {
  if (node === null) return;
  inorder(node.left);
  console.log(node.value);
  inorder(node.[1]);
}
Drag options to blanks, or click blank then click option'
Aroot
Bleft
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' instead of 'right' causes wrong traversal.
Using 'root' or 'parent' which are invalid here.
4fill in blank
hard

Fill both blanks to create a function that returns an array of inorder traversal values.

DSA Javascript
function inorderArray(node) {
  if (node === null) return [];
  return [...inorderArray(node.[1]), node.[2], ...inorderArray(node.right)];
}
Drag options to blanks, or click blank then click option'
Aleft
Bvalue
Cright
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right causes wrong order.
Using 'parent' or 'root' which are invalid.
5fill in blank
hard

Fill all three blanks to create a function that builds a map of node values to their inorder positions.

DSA Javascript
function inorderMap(node, map = new Map(), index = {count: 0}) {
  if (node === null) return map;
  inorderMap(node.[1], map, index);
  map.set(node.[2], index.[3]);
  index.count++;
  inorderMap(node.right, map, index);
  return map;
}
Drag options to blanks, or click blank then click option'
Aleft
Bvalue
Ccount
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'right' instead of 'left' in first blank.
Using 'parent' or 'root' which do not exist.
Using 'index' instead of 'count' for the counter.