Challenge - 5 Problems
Binary Tree Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this binary tree traversal?
Consider the following binary tree node structure and code that creates a simple tree and prints its in-order traversal. What will be printed?
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) print(inorder(root))
Attempts:
2 left
💡 Hint
In-order traversal visits left child, then node, then right child.
✗ Incorrect
In-order traversal of a binary tree visits nodes in ascending order for a binary search tree. The left subtree nodes (3, 5, 7) come first, then root (10), then right subtree (15).
🧠 Conceptual
intermediateWhat does the 'left' attribute in a binary tree node represent?
In a binary tree node structure, what does the 'left' attribute usually represent?
Attempts:
2 left
💡 Hint
Think about the direction 'left' in a tree structure.
✗ Incorrect
The 'left' attribute points to the node's left child, which is the subtree or node connected on the left side.
❓ Predict Output
advancedWhat error does this binary tree node code raise?
What error will this code raise when executed?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(1) print(root.left.val)
Attempts:
2 left
💡 Hint
Check what root.left is before accessing val.
✗ Incorrect
root.left is None, so trying to access val attribute on None raises AttributeError.
🚀 Application
advancedHow many nodes are in this binary tree?
Given the following binary tree node structure and code, how many nodes does the tree contain?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(8) root.left = Node(3) root.right = Node(10) root.left.left = Node(1) root.left.right = Node(6) root.left.right.left = Node(4) root.left.right.right = Node(7) root.right.right = Node(14) root.right.right.left = Node(13) def count_nodes(node): if node is None: return 0 return 1 + count_nodes(node.left) + count_nodes(node.right) print(count_nodes(root))
Attempts:
2 left
💡 Hint
Count each node including root and all children recursively.
✗ Incorrect
The tree has 9 nodes total: 8,3,10,1,6,4,7,14,13.
🧠 Conceptual
expertWhat is the height of this binary tree?
Given the binary tree constructed below, what is its height? Height is the number of edges on the longest path from root to a leaf.
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(20) root.left = Node(10) root.right = Node(30) root.left.left = Node(5) root.left.left.left = Node(2) root.right.right = Node(40) root.right.right.right = Node(50) root.right.right.right.right = Node(60)
Attempts:
2 left
💡 Hint
Count edges from root to deepest leaf node.
✗ Incorrect
The longest path is root(20) -> 30 -> 40 -> 50 -> 60 with 4 edges.
