🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Tree Traversals: all 13 questions

Quiz: Tree Traversals · Question 11 of 13

What does this print? (code)

What does this print?

class Node:
    def __init__(self, val, left=None, right=None):
        self.val, self.left, self.right = val, left, right

def height(n):
    if n is None:
        return 0
    return 1 + max(height(n.left), height(n.right))

root = Node(1, Node(2, Node(4, Node(7))), Node(3, None, Node(6)))
print(height(root))
  1. A.3
  2. B.7
  3. C.2
  4. D.4
Answer & explanation

Correct answer: 4

The longest root-to-leaf path is 1 → 2 → 4 → 7, which has 4 nodes.

Can you get the rest right without peeking?

Take the timed Quiz: Tree Traversals quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →