Machine-translated by Google. Questions are authored in English.
Top

Trees and Binary Trees Questions and Answers

1.

Consider the following New-order strategy for traversing a binary tree:

  • Visit the root;
  • Visit the right subtree using New-order;
  • Visit the left subtree using New-order;

The New-order traversal of the expression tree corresponding to the reverse polish expression 3 4 * 5 - 2 ^ 6 7 * 1 + - is given by:

Exam Year(s)

GATE CS - 2016,

Answer: C

The New-order traversal is - + 1 * 7 6 ^ 2 - 5 * 4 3.

Build the tree from the postfix expression 3 4 * 5 - 2 ^ 6 7 * 1 + -: it is ((3 * 4) - 5) ^ 2 - ((6 * 7) + 1). The root is the last operator, -, with left subtree ((3*4)-5) ^ 2 and right subtree (6*7)+1.

New-order visits root, then right subtree, then left subtree, recursively:

  • Root: -
  • Right subtree (6*7)+1: +, then its right child 1, then its left subtree * 7 6 (root, right, left).
  • Left subtree: ^, then its right child 2, then its left subtree -, 5, then * 4 3.

This gives - + 1 * 7 6 ^ 2 - 5 * 4 3. New-order is exactly the reverse of postfix order, which is a quick check: reverse 3 4 * 5 - 2 ^ 6 7 * 1 + - and you get the same string. The option with * 6 7 and * 3 4 visits the left child before the right in the inner subtrees, and the other two options start with the wrong node.

Please log in or register to report this question.

2.

Consider a binary tree \(T\) in which every node has either zero or two children. Let \(n \gt 0\) be the number of nodes in \(T\).

Which ONE of the following is the number of nodes in \(T\) that have exactly two children?

Exam Year(s)

GATE CS - 2025,

Answer: B

The number of nodes with two children is \(\dfrac{n-1}{2}\).

In a full binary tree, let i be the number of internal nodes (two children each) and l the number of leaves. Counting edges two ways: every node except the root has exactly one parent edge, so there are n - 1 edges, and each internal node contributes 2 edges, so there are 2i edges. Hence:

\[ 2i = n - 1 \quad \Rightarrow \quad i = \frac{n-1}{2} \]

A check: a root with two leaf children has n = 3 and i = 1 = (3 - 1)/2. (n + 1)/2 is the number of leaves, the usual confusion, and n/2 or (n - 2)/2 are not even integers for the odd n that a full binary tree always has.

Please log in or register to report this question.

3.

The set T represents various traversals over binary tree. The set S represents the order of visiting nodes during a traversal.

TS
I: InorderL: left subtree, node, right subtree
II: PreorderM: node, left subtree, right subtree
III: PostorderN: left subtree, right subtree, node

Which one of the following is the correct match from T to S ?

Exam Year(s)

GATE CS - 2026,

Answer: A

The correct match is I - L, II - M, III - N.

The names say where the node is visited relative to its subtrees:

  • Inorder: left subtree, node, right subtree (L). For a binary search tree this lists the keys in sorted order.
  • Preorder: node first, then left and right subtrees (M).
  • Postorder: left and right subtrees first, node last (N).

Every other option assigns at least one traversal the wrong position for the node: matching inorder to 'node, left, right' or postorder to 'left, node, right' confuses the prefixes pre, in and post, which refer exactly to that position.

Please log in or register to report this question.

4.

Consider the C function foo and the binary tree shown.

typedef struct node {
int val;
struct node *left, *right;
} node;

int foo(node *p) {
int retval;
if (p == NULL)
return 0;
else {
retval = p-\(\gt\)val + foo(p-\(\gt\)left) + foo(p-\(\gt\)right);
printf("%d ", retval);
return retval;
}
}

Binary tree with root 10; left child 5 with children 3 and 8; right child 11 whose only child, on its right, is 13

When foo is called with a pointer to the root node of the given binary tree, what will it print?

Exam Year(s)

GATE CS - 2023,

Answer: C

The program prints 3 8 16 13 24 50.

foo computes the sum of the values in the subtree rooted at p, and it prints that sum only after both recursive calls have returned, so the subtrees are printed in post-order: left subtree, right subtree, then the node.

  • Node 3: prints 3. Node 8: prints 8.
  • Node 5: 5 + 3 + 8 = 16, printed.
  • Node 13: prints 13. Node 11: 11 + 0 + 13 = 24, printed.
  • Root 10: 10 + 16 + 24 = 50, printed.

'3 8 5 13 11 10' is the post-order of the node values, which would print val rather than the subtree sums. '3 5 8 10 11 13' is the in-order sequence. '3 16 8 50 24 13' has the right sums but in an in-order arrangement that the code does not produce.

Please log in or register to report this question.

5.

The pre-order and post-order traversal of a binary tree generates the same output. The tree can have maximum :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

The tree can have at most one node.

Pre-order visits the root first and then its subtrees; post-order visits the subtrees first and the root last. In any tree with two or more nodes, the root is the first node of the pre-order output but the last node of the post-order output, so the two sequences differ.

Only a tree with a single node, where the root is both first and last, gives the same output in both traversals. So two nodes, three nodes and any number of nodes are all wrong.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Trees and Binary Trees Questions and Answers || Trees and Binary Trees MCQ Questions and Answers || Trees and Binary Trees GK Questions and Answers || Trees and Binary Trees GK MCQ Questions || Trees and Binary Trees Multiple Choice Questions and Answers || Trees and Binary Trees GK || GK on Trees and Binary Trees || Data Structures and Algorithms Questions and Answers || Data Structures and Algorithms MCQ Questions and Answers || Data Structures and Algorithms GK Questions and Answers || GK on Data Structures and Algorithms