Challenge - 5 Problems
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the inorder traversal output of this binary tree?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number, left: Node | null = null, right: Node | null = null) { this.val = val; this.left = left; this.right = right; } } const root = new Node(2, new Node(1), new Node(3)); function inorderTraversal(root: Node | null): number[] { if (!root) return []; return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)]; } console.log(inorderTraversal(root));
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left child first, then root, then right child.
✗ Incorrect
Inorder traversal visits nodes in the order: left subtree, root, right subtree.
For the tree with root 2, left child 1, and right child 3, the order is 1, 2, 3.
❓ Predict Output
intermediate2:00remaining
What is the inorder traversal output of this unbalanced tree?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number, left: Node | null = null, right: Node | null = null) { this.val = val; this.left = left; this.right = right; } } const root = new Node(4, new Node(2, new Node(1), new Node(3)), new Node(5)); function inorderTraversal(root: Node | null): number[] { if (!root) return []; return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)]; } console.log(inorderTraversal(root));
Attempts:
2 left
💡 Hint
Traverse left subtree fully before root, then right subtree.
✗ Incorrect
Inorder traversal visits left subtree nodes first, then root, then right subtree nodes.
For this tree, the order is 1, 2, 3, 4, 5.
🔧 Debug
advanced2:00remaining
Why does this inorder traversal code cause a runtime error?
Find the cause of the runtime error in this inorder traversal function.
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number, left: Node | null = null, right: Node | null = null) { this.val = val; this.left = left; this.right = right; } } function inorderTraversal(root: Node | null): number[] { if (root === null) return []; const left = inorderTraversal(root.left); const right = inorderTraversal(root.right); return left.concat([root.val]).concat(right); } const root = new Node(1); console.log(inorderTraversal(root));
Attempts:
2 left
💡 Hint
Check the types passed to concat method.
✗ Incorrect
The concat method expects an array argument, but root.val is a number.
This causes a TypeError at runtime.
❓ Predict Output
advanced2:00remaining
What is the inorder traversal output of this tree with null children?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
DSA Typescript
class Node { val: number; left: Node | null; right: Node | null; constructor(val: number, left: Node | null = null, right: Node | null = null) { this.val = val; this.left = left; this.right = right; } } const root = new Node(10, null, new Node(20, new Node(15), null)); function inorderTraversal(root: Node | null): number[] { if (!root) return []; return [...inorderTraversal(root.left), root.val, ...inorderTraversal(root.right)]; } console.log(inorderTraversal(root));
Attempts:
2 left
💡 Hint
Remember to visit left subtree before root, even if left is null.
✗ Incorrect
Inorder traversal visits left subtree, root, then right subtree.
Here, left of root is null, so visit root 10, then traverse right subtree which visits 15 then 20.
🧠 Conceptual
expert2:00remaining
How many nodes are visited before the root in inorder traversal of a complete binary tree of height 3?
Consider a complete binary tree of height 3 (levels 0 to 3). How many nodes are visited before the root node during an inorder traversal?
Attempts:
2 left
💡 Hint
Count all nodes in the left subtree of the root.
✗ Incorrect
In inorder traversal, all nodes in the left subtree are visited before the root.
A complete binary tree of height 3 has 7 nodes in the left subtree (levels 1 and 2 fully filled).