Challenge - 5 Problems
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the inorder traversal output of this binary tree?
Given the binary tree constructed below, what is the printed inorder traversal (Left, Root, Right)?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder(root): if root is None: return [] return inorder(root.left) + [root.val] + inorder(root.right) root = Node(10) root.left = Node(5) root.right = Node(15) root.left.left = Node(3) root.left.right = Node(7) root.right.right = Node(18) result = inorder(root) print(result)
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree first, then root, then right subtree.
✗ Incorrect
Inorder traversal visits nodes in the order: left child, root, right child. So for this tree, the order is 3 (leftmost), 5 (parent of 3 and 7), 7, 10 (root), 15, 18.
❓ Predict Output
intermediateWhat is the inorder traversal output of this unbalanced tree?
Given the binary tree below, what is the inorder traversal output?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder(root): if root is None: return [] return inorder(root.left) + [root.val] + inorder(root.right) root = Node(8) root.left = Node(3) root.left.right = Node(6) root.left.right.left = Node(4) root.left.right.right = Node(7) root.right = Node(10) root.right.right = Node(14) root.right.right.left = Node(13) result = inorder(root) print(result)
Attempts:
2 left
💡 Hint
Follow left subtree fully before root, then right subtree.
✗ Incorrect
Inorder traversal visits left subtree nodes first, then root, then right subtree nodes. The correct order is 3, 4, 6, 7, 8, 10, 13, 14.
🔧 Debug
advancedWhat error does this inorder traversal code raise?
Consider the following code for inorder traversal. What error will it raise when run?
DSA Python
def inorder(root): if root is None: return inorder(root.left) print(root.val) inorder(root.right) root = None inorder(root)
Attempts:
2 left
💡 Hint
Check what happens when root is None at the start.
✗ Incorrect
The function returns immediately when root is None, so no output and no error occurs.
❓ Predict Output
advancedWhat is the inorder traversal output of this tree with duplicate values?
Given the binary tree below with duplicate values, what is the inorder traversal output?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def inorder(root): if root is None: return [] return inorder(root.left) + [root.val] + inorder(root.right) root = Node(5) root.left = Node(3) root.right = Node(5) root.left.left = Node(3) root.left.right = Node(4) root.right.right = Node(7) result = inorder(root) print(result)
Attempts:
2 left
💡 Hint
Duplicates appear in the order they are visited in inorder traversal.
✗ Incorrect
Inorder traversal visits left subtree first, so the two 3s come first, then 4, then root 5, then right 5 and 7.
🧠 Conceptual
expertHow many nodes are visited in inorder traversal of a full binary tree with height 3?
A full binary tree is a tree where every node has 0 or 2 children. If the tree height is 3 (root at height 1), how many nodes will the inorder traversal visit?
Attempts:
2 left
💡 Hint
Number of nodes in a full binary tree of height h is 2^h - 1.
✗ Incorrect
A full binary tree of height 3 has 2^3 - 1 = 7 nodes. Inorder traversal visits all nodes exactly once.
