0
0
DSA Javascriptprogramming~20 mins

Create a Binary Tree Manually in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Tree Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output after inserting nodes manually?
Consider the following JavaScript code that creates a binary tree by manually linking nodes. What will be the printed output of the tree in in-order traversal (left, root, right)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right = new Node(18);

function inorder(node) {
  if (!node) return [];
  return [...inorder(node.left), node.value, ...inorder(node.right)];
}

console.log(inorder(root).join(' -> ') + ' -> null');
A3 -> 7 -> 5 -> 10 -> 18 -> 15 -> null
B10 -> 5 -> 3 -> 7 -> 15 -> 18 -> null
C3 -> 5 -> 7 -> 10 -> 15 -> 18 -> null
D5 -> 3 -> 7 -> 10 -> 15 -> 18 -> null
Attempts:
2 left
💡 Hint
In-order traversal visits left child, then node, then right child.
🧠 Conceptual
intermediate
1:30remaining
How many nodes are in this manually created binary tree?
Given the following manual binary tree creation code, how many nodes does the tree contain?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
A7
B6
C5
D8
Attempts:
2 left
💡 Hint
Count each new Node created and linked.
🔧 Debug
advanced
2:00remaining
What error does this manual binary tree creation code produce?
Examine the code below that attempts to create a binary tree manually. What error will it raise when run?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(3);
root.left.right = new Node(7);
root.right.right.left = new Node(18); // Problematic line
AReferenceError: root.right.right is not defined
BTypeError: Cannot set property 'left' of null
CSyntaxError: Unexpected token '.'
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check if all intermediate nodes exist before assigning children.
Predict Output
advanced
2:00remaining
What is the output of this pre-order traversal of a manually created binary tree?
Given the following binary tree created manually, what is the output of the pre-order traversal (root, left, right)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(8);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(1);
root.left.right = new Node(6);
root.left.right.left = new Node(4);
root.left.right.right = new Node(7);
root.right.right = new Node(14);
root.right.right.left = new Node(13);

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

console.log(preorder(root).join(' -> ') + ' -> null');
A8 -> 3 -> 1 -> 6 -> 4 -> 7 -> 10 -> 14 -> 13 -> null
B1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 13 -> 14 -> null
C3 -> 1 -> 6 -> 4 -> 7 -> 8 -> 10 -> 14 -> 13 -> null
D8 -> 3 -> 6 -> 1 -> 7 -> 4 -> 10 -> 14 -> 13 -> null
Attempts:
2 left
💡 Hint
Pre-order visits root first, then left subtree, then right subtree.
🧠 Conceptual
expert
2:30remaining
What is the height of this manually created binary tree?
Consider the binary tree created manually below. What is the height of this tree? Height is the number of edges on the longest path from root to a leaf.
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new Node(20);
root.left = new Node(10);
root.right = new Node(30);
root.left.left = new Node(5);
root.left.left.left = new Node(2);
root.right.right = new Node(40);
root.right.right.right = new Node(50);
root.right.right.right.right = new Node(60);
A5
B3
C6
D4
Attempts:
2 left
💡 Hint
Count edges from root to the deepest leaf node.