0
0
DSA Javascriptprogramming~10 mins

Tree Traversal Preorder Root Left 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 root node first in preorder traversal.

DSA Javascript
function preorder(node) {
  if (node == null) return;
  console.log(node.value);
  preorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Using .right instead of .left causes wrong traversal order.
Trying to access .parent instead of children.
2fill in blank
medium

Complete the code to visit the right child after the left child in preorder traversal.

DSA Javascript
function preorder(node) {
  if (node == null) return;
  console.log(node.value);
  preorder(node.left);
  preorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.right
B.left
C.parent
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting .left twice instead of .right second time.
Trying to visit .parent or .child which are not correct.
3fill in blank
hard

Fix the error in the preorder traversal code to correctly visit root, left, then right.

DSA Javascript
function preorder(node) {
  if (node == null) return;
  console.log(node.value);
  preorder(node.left);
  preorder(node[1]);
}
Drag options to blanks, or click blank then click option'
A.left
B.right
C.parent
D.child
Attempts:
3 left
💡 Hint
Common Mistakes
Calling preorder on .left twice.
Using .parent or .child which are invalid here.
4fill in blank
hard

Fill both blanks to complete the preorder traversal visiting root, left, then right.

DSA Javascript
function preorder(node) {
  if (node == null) return;
  console.log(node[1]);
  preorder(node[2]);
  preorder(node.right);
}
Drag options to blanks, or click blank then click option'
A.value
B.left
C.right
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Printing .left or .right instead of .value.
Visiting .right before .left.
5fill in blank
hard

Fill all three blanks to complete preorder traversal: print root, visit left, then right.

DSA Javascript
function preorder(node) {
  if (node == null) return;
  console.log(node[1]);
  preorder(node[2]);
  preorder(node[3]);
}
Drag options to blanks, or click blank then click option'
A.value
B.left
C.right
D.parent
Attempts:
3 left
💡 Hint
Common Mistakes
Visiting right child before left child.
Printing .left or .right instead of .value.