Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Binary Tree Node Structure in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Binary Tree Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Binary Tree Node Creation
What is the output of the following JavaScript code that creates a simple binary tree node and prints its structure?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);

console.log(JSON.stringify(root));
A{"value":10,"left":{"value":5,"left":null,"right":null},"right":{"value":15,"left":null,"right":null}}
B{"value":10,"left":5,"right":15}
C{"value":10,"left":null,"right":null}
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Think about how the TreeNode class stores left and right children as objects, not just values.
❓ Predict Output
intermediate
2:00remaining
Value of Left Child Node
After running this code, what is the value of the left child node of the root?
DSA Javascript
function TreeNode(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}

const root = new TreeNode(20);
root.left = new TreeNode(10);
root.right = new TreeNode(30);

const leftValue = root.left.value;
console.log(leftValue);
A10
B20
C30
Dnull
Attempts:
2 left
💡 Hint
Look at what value is assigned to root.left.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Tree Node Assignment
What error will this code produce when run?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(5);
root.left.value = 3;
console.log(root.left.value);
Aundefined
B3
CTypeError: Cannot set property 'value' of null
DSyntaxError
Attempts:
2 left
💡 Hint
Check if root.left is initialized before setting its value.
❓ Predict Output
advanced
2:00remaining
Output of Nested Binary Tree Node Structure
What is the output of this code that creates a nested binary tree and prints the root's right child's left child's value?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(1);
root.right = new TreeNode(2);
root.right.left = new TreeNode(3);

console.log(root.right.left.value);
A1
B3
Cnull
D2
Attempts:
2 left
💡 Hint
Trace the path from root to right child, then to left child.
🧠 Conceptual
expert
3:00remaining
Number of Nodes in a Binary Tree
Given a binary tree node structure where each node has 'left' and 'right' pointers, what is the total number of nodes after executing this code?
DSA Javascript
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

const root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(7);
root.right.right = new TreeNode(18);
A4
B5
C7
D6
Attempts:
2 left
💡 Hint
Count each node created including root and all children.

Practice

(1/5)
1. What does a typical binary tree node contain in its structure?
easy
A. A value and multiple child nodes in a list
B. A value, a left child node, and a right child node
C. Only a value and a pointer to the next node
D. Only a value without any child references

Solution

  1. Step 1: Understand binary tree node components

    A binary tree node stores a value and has exactly two child references: left and right.
  2. Step 2: Compare options with binary tree definition

    A value, a left child node, and a right child node correctly lists value, left child, and right child. Others describe linked list or general tree nodes.
  3. Final Answer:

    A value, a left child node, and a right child node -> Option B
  4. Quick Check:

    Binary tree node = value + left + right [OK]
Hint: Binary tree nodes always have left and right child pointers [OK]
Common Mistakes:
  • Confusing binary tree node with linked list node
  • Thinking nodes have multiple children like general trees
  • Ignoring child pointers in node structure
2. Which of the following is the correct JavaScript syntax to define a binary tree node class?
easy
A. class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } }
B. function Node(value) { this.value = value; this.next = null; }
C. class Node { constructor(value) { this.value = value; this.children = []; } }
D. var Node = (value) => { this.value = value; this.left = null; this.right = null; }

Solution

  1. Step 1: Identify correct class syntax for binary tree node

    class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } uses ES6 class with constructor setting value, left, and right to null.
  2. Step 2: Check other options for errors

    function Node(value) { this.value = value; this.next = null; } uses next pointer (linked list), C uses children array (general tree), D uses arrow function incorrectly for constructor.
  3. Final Answer:

    class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } -> Option A
  4. Quick Check:

    Class with value, left, right = class Node { constructor(value) { this.value = value; this.left = null; this.right = null; } } [OK]
Hint: Use ES6 class with left and right set to null [OK]
Common Mistakes:
  • Using 'next' instead of 'left' and 'right'
  • Using arrow function as constructor
  • Using children array instead of left/right
3. What will be the output of the following code?
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);
console.log(root.left.value + ' -> ' + root.value + ' -> ' + root.right.value);
medium
A. "5 -> 10 -> 15"
B. "10 -> 5 -> 15"
C. "null -> 10 -> null"
D. "15 -> 10 -> 5"

Solution

  1. Step 1: Trace node creation and assignments

    Root node value is 10, left child is 5, right child is 15.
  2. Step 2: Evaluate console.log output

    Output concatenates left.value (5), root.value (10), right.value (15) with arrows.
  3. Final Answer:

    "5 -> 10 -> 15" -> Option A
  4. Quick Check:

    Left 5, root 10, right 15 = "5 -> 10 -> 15" [OK]
Hint: Print left, root, right values in order [OK]
Common Mistakes:
  • Mixing order of values in output
  • Confusing left and right child values
  • Forgetting to access .value property
4. Identify the error in the following binary tree node code snippet:
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 = 15;
console.log(root.left.left.value);
medium
A. Missing constructor in Node class
B. Using console.log incorrectly
C. Assigning a number directly instead of a Node object to left.left
D. root.right is not assigned properly

Solution

  1. Step 1: Analyze assignment to root.left.left

    root.left.left is assigned 15 (a number), not a Node instance.
  2. Step 2: Understand why console.log fails

    Accessing .value on a number (15) causes error because 15 has no .value property.
  3. Final Answer:

    Assigning a number directly instead of a Node object to left.left -> Option C
  4. Quick Check:

    Child nodes must be Node objects, not raw values [OK]
Hint: Child nodes must be Node instances, not raw values [OK]
Common Mistakes:
  • Assigning raw values instead of Node objects
  • Ignoring error from accessing .value on number
  • Assuming constructor is missing
5. Given the following binary tree node class, how would you write a function to count the total number of nodes in the tree starting from the root?
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

Choose the correct function implementation.
hard
A. function countNodes(node) { if (node === null) return null; return 1 + countNodes(node.left) + countNodes(node.right); }
B. function countNodes(node) { if (!node) return 1; return countNodes(node.left) + countNodes(node.right); }
C. function countNodes(node) { return node.value + countNodes(node.left) + countNodes(node.right); }
D. function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); }

Solution

  1. Step 1: Understand base case for recursion

    If node is null, count is zero because no node exists there.
  2. Step 2: Sum current node plus counts of left and right subtrees

    Return 1 (current node) plus recursive counts of left and right children.
  3. Step 3: Check options for correct base case and sum

    function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } correctly returns 0 for null and sums 1 + left + right counts.
  4. Final Answer:

    function countNodes(node) { if (!node) return 0; return 1 + countNodes(node.left) + countNodes(node.right); } -> Option D
  5. Quick Check:

    Count nodes = 1 + left count + right count [OK]
Hint: Return 0 for null, else 1 + left + right counts [OK]
Common Mistakes:
  • Returning 1 for null node instead of 0
  • Adding node.value instead of count 1
  • Returning null instead of 0 in base case
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard