Skip to content
Official PYQ · 2000

GATE CS 2000 Q1.14 — Trees

Explain with AI

Consider the following nested representation of binary trees: (X Y Z)(X \ Y \ Z) indicates YY and ZZ are the left and right subtrees, respectively, of node XX. Note that YY and ZZ may be NULLNULL, or further nested. Which of the following represents a valid binary tree?

Choose one option
View worked solution

Answer: C

According to the problem definition, a binary tree is represented in the nested format as (X Y Z)(X \ Y \ Z), where:

  • XX is the root node.
  • YY is the left subtree (which can be a single node, NULLNULL, or another nested tree of the form (A B C)(A \ B \ C)).
  • ZZ is the right subtree (which can be a single node, NULLNULL, or another nested tree of the form (A B C)(A \ B \ C)).

This means every nested parenthesis representing a subtree must contain exactly three components: the root, the left child/subtree, and the right child/subtree.

Let's evaluate each option:

  • Option A: (1 2 (4 5 6 7))(1 \ 2 \ (4 \ 5 \ 6 \ 7))
    The right subtree (4 5 6 7)(4 \ 5 \ 6 \ 7) contains four elements inside the parentheses, which violates the (X Y Z)(X \ Y \ Z) format. Thus, it is invalid.
  • Option B: (1 (2 3 4) 5 6) 7)(1 \ (2 \ 3 \ 4) \ 5 \ 6) \ 7)
    This expression has mismatched parentheses and contains too many components at the outer level. Thus, it is invalid.
  • Option C: (1 (2 3 4) (5 6 7))(1 \ (2 \ 3 \ 4) \ (5 \ 6 \ 7))
    This matches the format (X Y Z)(X \ Y \ Z) perfectly:
    • Root X=1X = 1
    • Left subtree Y=(2 3 4)Y = (2 \ 3 \ 4), which is a valid binary tree with root 22, left child 33, and right child 44.
    • Right subtree Z=(5 6 7)Z = (5 \ 6 \ 7), which is a valid binary tree with root 55, left child 66, and right child 77.
    Hence, this is a valid binary tree.
  • Option D: (1 (2 3 NULL) (4 5))(1 \ (2 \ 3 \ NULL) \ (4 \ 5))
    The right subtree (4 5)(4 \ 5) contains only two elements inside the parentheses, violating the (X Y Z)(X \ Y \ Z) format. Thus, it is invalid.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Trees questions