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 will be the printed output of the binary tree node after running the code below?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } const root = new TreeNode(10); root.left = new TreeNode(5); root.right = new TreeNode(15); console.log(root.val + ' -> ' + root.left?.val + ' -> ' + root.right?.val + ' -> null');
Attempts:
2 left
💡 Hint
Think about how the root node and its children are assigned values.
✗ Incorrect
The root node has value 10, its left child is 5, and right child is 15. The print statement shows these values in order followed by null.
❓ Predict Output
intermediate2:00remaining
Value of Left Child After Node Modification
After running the code below, what is the value of root.left.val?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } const root = new TreeNode(20); root.left = new TreeNode(10); root.left.val = 15;
Attempts:
2 left
💡 Hint
Look at the line where root.left.val is changed.
✗ Incorrect
The left child's value was initially 10 but then changed to 15 explicitly.
❓ Predict Output
advanced3:00remaining
Output of Inorder Traversal of Binary Tree
What is the output of the inorder traversal of the binary tree created by the code below?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } function inorder(root: TreeNode | null, result: number[] = []): number[] { if (root === null) return result; inorder(root.left, result); result.push(root.val); inorder(root.right, result); return result; } const root = new TreeNode(8); root.left = new TreeNode(3); root.right = new TreeNode(10); root.left.left = new TreeNode(1); root.left.right = new TreeNode(6); root.right.right = new TreeNode(14); const output = inorder(root); console.log(output.join(' -> ') + ' -> null');
Attempts:
2 left
💡 Hint
Inorder traversal visits left child, then node, then right child.
✗ Incorrect
Inorder traversal visits nodes in ascending order for binary search trees. The sequence is left subtree (1,3,6), root (8), right subtree (10,14).
🔧 Debug
advanced2:00remaining
Identify the Error in Binary Tree Node Assignment
What error will the following code produce when run?
DSA Typescript
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } const root = new TreeNode(5); root.left.val = 3;
Attempts:
2 left
💡 Hint
Check if root.left is assigned before accessing its val property.
✗ Incorrect
root.left is null by default. Trying to access val on null causes a TypeError.
🧠 Conceptual
expert3:00remaining
Number of Nodes in a Complete Binary Tree of Height h
What is the total number of nodes in a complete binary tree of height h (where height is the number of edges from root to deepest leaf)?
Attempts:
2 left
💡 Hint
Think about how nodes double at each level in a complete binary tree.
✗ Incorrect
A complete binary tree of height h has nodes equal to sum of nodes at each level: 1 + 2 + 4 + ... + 2^h = 2^(h+1) - 1.