0
0
DSA Javascriptprogramming~20 mins

Tree Traversal Preorder Root Left Right in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this preorder traversal?
Given the binary tree below, what is the output of the preorder traversal (Root, Left, Right)?
DSA Javascript
class Node {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(10,
  new Node(5,
    new Node(3),
    new Node(7)
  ),
  new Node(15,
    null,
    new Node(18)
  )
);

function preorder(node, result = []) {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root).join(' -> ') + ' -> null');
A10 -> 5 -> 3 -> 7 -> 15 -> 18 -> null
B3 -> 5 -> 7 -> 10 -> 15 -> 18 -> null
C10 -> 15 -> 18 -> 5 -> 3 -> 7 -> null
D5 -> 3 -> 7 -> 10 -> 15 -> 18 -> null
Attempts:
2 left
💡 Hint
Remember preorder visits root first, then left subtree, then right subtree.
Predict Output
intermediate
1:00remaining
Output of preorder traversal on a single-node tree
What is the output of the preorder traversal on this tree with only one node?
DSA Javascript
class Node {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(42);

function preorder(node, result = []) {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root).join(' -> ') + ' -> null');
A42 -> 42 -> null
B42
C42 -> null
Dnull
Attempts:
2 left
💡 Hint
Preorder always includes the root node first.
🔧 Debug
advanced
1:30remaining
Identify the error in this preorder traversal function
What error will this preorder traversal code produce when run?
DSA Javascript
function preorder(node, result = []) {
  if (node == null) return;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

const root = null;
console.log(preorder(root));
ATypeError
B[1]
Cnull
Dundefined
Attempts:
2 left
💡 Hint
Check what happens when the function returns without a value.
Predict Output
advanced
2:00remaining
Preorder traversal output with unbalanced tree
What is the output of the preorder traversal for this unbalanced tree?
DSA Javascript
class Node {
  constructor(value, left = null, right = null) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

const root = new Node(1,
  new Node(2,
    new Node(4),
    null
  ),
  new Node(3,
    null,
    new Node(5,
      null,
      new Node(6)
    )
  )
);

function preorder(node, result = []) {
  if (!node) return result;
  result.push(node.value);
  preorder(node.left, result);
  preorder(node.right, result);
  return result;
}

console.log(preorder(root).join(' -> ') + ' -> null');
A1 -> 3 -> 5 -> 6 -> 2 -> 4 -> null
B1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null
C4 -> 2 -> 1 -> 3 -> 5 -> 6 -> null
D1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint
Preorder visits root first, then left subtree, then right subtree.
🧠 Conceptual
expert
1:30remaining
How many nodes are visited in preorder traversal of a full binary tree with height 3?
A full binary tree is a tree where every node has 0 or 2 children. If the tree has height 3 (root is height 1), how many nodes will the preorder traversal visit?
A7
B15
C3
D8
Attempts:
2 left
💡 Hint
Number of nodes in a full binary tree of height h is 2^h - 1.