Challenge - 5 Problems
Binary Tree Node Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Think about how the TreeNode class stores left and right children as objects, not just values.
✗ Incorrect
The TreeNode class creates nodes with value, left, and right properties. Left and right are either null or TreeNode objects. The JSON.stringify prints the full nested structure.
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Look at what value is assigned to root.left.
✗ Incorrect
The left child node is created with value 10, so root.left.value is 10.
🔧 Debug
advanced2: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);
Attempts:
2 left
💡 Hint
Check if root.left is initialized before setting its value.
✗ Incorrect
root.left is null initially, so trying to set root.left.value causes a TypeError.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Trace the path from root to right child, then to left child.
✗ Incorrect
root.right.left is a TreeNode with value 3, so printing its value outputs 3.
🧠 Conceptual
expert3: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);
Attempts:
2 left
💡 Hint
Count each node created including root and all children.
✗ Incorrect
Nodes created: root(1), root.left(2), root.right(3), root.left.left(4), root.left.right(5), root.right.right(6). Total 6 nodes.