0
0
Intro to Computingfundamentals~20 mins

Trees and hierarchical data in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tree Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
trace
intermediate
2:00remaining
Trace the output of a simple tree traversal

Consider a tree where each node has a value and up to two children (left and right). The traversal visits nodes in this order: root, left child, right child.

What is the output sequence of node values for the tree below?

    10
   /  \
  5    15
 /    /  \
3    12  20
Intro to Computing
def preorder(node):
    if node is None:
        return []
    return [node.value] + preorder(node.left) + preorder(node.right)

# Tree structure:
# Node(10, left=Node(5, left=Node(3)), right=Node(15, left=Node(12), right=Node(20)))

# Call preorder(root)
A[5, 3, 10, 15, 12, 20]
B[3, 5, 10, 12, 15, 20]
C[10, 15, 20, 12, 5, 3]
D[10, 5, 3, 15, 12, 20]
Attempts:
2 left
💡 Hint

Preorder traversal visits the root node first, then recursively visits the left subtree, then the right subtree.

🧠 Conceptual
intermediate
1:30remaining
Identify the correct tree property

Which of the following statements correctly describes a binary search tree (BST)?

AFor every node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater.
BEvery node has exactly two children, and the tree is perfectly balanced.
CNodes are arranged so that the sum of values in the left subtree equals the sum in the right subtree.
DThe tree is stored as a linked list with nodes connected only to their next sibling.
Attempts:
2 left
💡 Hint

Think about the ordering rule that defines a binary search tree.

Comparison
advanced
1:30remaining
Compare tree traversal outputs

Given the same binary tree, which traversal method produces the nodes in ascending order if the tree is a binary search tree?

APreorder traversal (root, left, right)
BPostorder traversal (left, right, root)
CInorder traversal (left, root, right)
DLevel-order traversal (breadth-first)
Attempts:
2 left
💡 Hint

Consider which traversal visits nodes in sorted order for a BST.

identification
advanced
2:00remaining
Identify the error in tree node insertion

Consider the following pseudocode for inserting a value into a binary search tree:

function insert(node, value):
  if node is None:
    return new Node(value)
  if value < node.value:
    insert(node.left, value)
  else:
    insert(node.right, value)
  return node

What is the main problem with this code?

AIt does not update the left or right child pointers after recursive insertion.
BIt returns None instead of the updated node.
CIt creates a new node even when the current node is not None.
DIt incorrectly compares values using less than instead of greater than.
Attempts:
2 left
💡 Hint

Think about how recursive calls affect the tree structure and what must be done to keep the tree updated.

🚀 Application
expert
2:30remaining
Calculate the height of a tree

Given the following tree structure, what is the height of the tree?

        A
       / \
      B   C
     /   / \
    D   E   F
       /     \
      G       H
               \
                I

Height is defined as the number of edges on the longest path from the root to a leaf.

A5
B4
C3
D6
Attempts:
2 left
💡 Hint

Trace the longest path from root A to the deepest leaf node.