The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
1.2k views

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

  1. $(1 \ 2 \ (4 \ 5 \ 6 \ 7))$
  2. $(1 \ (2 \ 3 \ 4) \ 5 \ 6) \ 7)$
  3. $(1 \ (2 \ 3  \ 4) \ (5 \ 6  \ 7))$
  4. $(1 \ (2 \ 3 \ NULL) \ (4 \ 5))$
asked in DS by Veteran (68.8k points)
edited by | 1.2k views

6 Answers

+27 votes
Best answer
  1. $\rightarrow  (4 \ 5 \ 6 \ 7)$ this part of answer is not correct. We have $(X \ Y \ Z)$ not $ (W \ X \ Y \ Z)$. SO this is wrong
  2. $\rightarrow 3$ closing paranthesis, $2$ opening paranthesis. This is wrong. 
  3. Correct $\rightarrow$
  4. $\rightarrow$ Here in $(1 \ (2 \ 3 \ NULL) \ (4 \ 5)) , (4 \ 5)$ this is not allowed. So this is wrong. (It should be $(4,5,NULL)$)
answered by Veteran (48.5k points)
edited by
In B) actually tree is not (XYZ) format.
Only parenthesis can be the correct [email protected] Sir
+5 votes

Option C is Ans  as it generates a valid binary tree as shown below fig.

No other option is eligible for given constraints of question.

               

 

answered by Veteran (22.7k points)
(X Y Z) indicates Y and Z are the left and right subtrees, respectively, of node X .... Its basically preorder sequence ...
+4 votes
ans c)
answered by Boss (5.1k points)
How did u solve that ?
+2 votes

To solve this question we have to look at two things: 1) In a binary tree, a node may have at most 2 children. 2) To construct binary tree from the given sequences above, innermost parenthesis should be worked first.

In Option A : ( 4 5 6 7 ) is there, which says that node 4 has got three children, which is wrong for a binary tree, and also in the question, only ( X Y Z ) is defined, i.e. a node X can have at most 2 children, which will be the roots of subtrees Y and Z.

In Option B : after working on innermost (2 3 4), where 2 is a node of the binary tree, 3 is left subtree of node 2 and 4 is right subtree of node 2. From this we get (1 2 5 6). Here 2 has come from the root of subtree ( 2 3 4 ). Now again we don't have any definition for ( 1 2 5 6). Hence invalid.

In Option C: after working on ( 2 3 4) and ( 5 6 7 ) we get ( 1 2 5 ) where 2 has come from the root of subtree ( 2 3 4 ) and 5 has come from the root of subtree ( 5 6 7 ). Now, in ( 1 2 5 ) node 1 is the root of the binary tree, and subtree with root 2 is the left subtree and subtree with root 5 is the right subtree at root node 1. Hence it is giving valid binary tree.

In Option D: It is given as ( 2 3 NULL), here N,U,L and L are given as different elements, which is again wrong as according to ( X Y Z) definition, a node can have at most 2 children.

Source :geeksforgeeks

answered by Loyal (3.2k points)
+1 vote
We can only construct from C option so c is answer
answered by Boss (9.6k points)
+1 vote
Ans: C valid binary tree
answered by Boss (7.4k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,443 questions
39,189 answers
108,813 comments
36,563 users