5,819 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))$

### Subscribe to GO Classes for GATE CSE 2022

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. $\text{CORRECT}$
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)$)

In B) actually tree is not (XYZ) format.
Only parenthesis can be the correct [email protected] Sir
nice akash sir,sir i think for option B) srestha mam's reason true
I have an doubt, in question they mention it was a binary tree,

When we construct the tree 234, 2 has left child 3, which is not valid,

in binary tree leftchild <= root && right child >= root.

In this case it was failing the basic condtion of binary tree.

Suppose if we construct tree 234 as  2(root)-> 3(RST) -> 4(RST), then it fails to be complete binary tree, but in qustn it was mentioned that it was complete tree.

Correct me if i am wrong and adds value if solution is provided.

@katari

What your definition says is the definition of Binary Search Tree and not of binary tree.

There is a difference between Binary tree and Binary search tree. Binary tree says every node has atmost 2 children thats it.

https://stackoverflow.com/questions/6380231/difference-between-binary-tree-and-binary-search-tree

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. (X Y Z) indicates Y and Z are the left and right subtrees, respectively, of node X .... Its basically preorder sequence ...
What will be the case for Option D

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

ans c)

### 1 comment

How did u solve that ?
We can only construct from C option so c is answer
Ans: C valid binary tree

by