Challenge - 5 Problems
Binary Tree Builder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this manual binary tree creation?
Consider the following Python code that creates a simple binary tree manually. What will be printed when we print the root node's left child's value?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(10) root.left = Node(5) root.right = Node(15) print(root.left.val)
Attempts:
2 left
💡 Hint
Look at which node is assigned to root.left and what its value is.
✗ Incorrect
The root node has value 10. Its left child is assigned a node with value 5. So printing root.left.val outputs 5.
❓ Predict Output
intermediateWhat is the output after adding a right child manually?
Given the code below, what will be printed when we print the value of root.right.right?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(1) root.right = Node(2) root.right.right = Node(3) print(root.right.right.val)
Attempts:
2 left
💡 Hint
Trace the assignments from root to right child and then to right-right child.
✗ Incorrect
root.right is node with value 2, and root.right.right is node with value 3. So printing root.right.right.val outputs 3.
🔧 Debug
advancedWhy does this manual binary tree code raise an error?
Look at the code below. It tries to create a binary tree manually but raises an error. What is the cause?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None root = Node(7) root.left.val = 3 print(root.left.val)
Attempts:
2 left
💡 Hint
Check if root.left is assigned a Node before accessing its val.
✗ Incorrect
root.left is None by default. Trying to access root.left.val causes AttributeError because None has no attribute val.
🧠 Conceptual
advancedHow many nodes are in this manually created binary tree?
Given the following code, how many nodes does the binary tree have?
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.right = Node(15) root.right.left = Node(25)
Attempts:
2 left
💡 Hint
Count each Node created and assigned.
✗ Incorrect
Nodes created: root(20), root.left(10), root.right(30), root.left.left(5), root.left.right(15), root.right.left(25). Total 6 nodes.
🚀 Application
expertWhat is the printed output of this manual binary tree traversal?
This code creates a binary tree manually and prints values in preorder (root, left, right). What is the output?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def preorder(node): if node is None: return print(node.val, end=' ') preorder(node.left) preorder(node.right) root = Node(8) root.left = Node(3) root.right = Node(10) root.left.left = Node(1) root.left.right = Node(6) root.right.right = Node(14) root.left.right.left = Node(4) root.left.right.right = Node(7) preorder(root)
Attempts:
2 left
💡 Hint
Preorder means print root first, then left subtree, then right subtree.
✗ Incorrect
Preorder traversal visits nodes in order: root(8), left subtree (3,1,6,4,7), right subtree (10,14). So output is '8 3 1 6 4 7 10 14 '.
