The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+23 votes

Consider the following rooted tree with the vertex labeled $P$ as the root:

The order in which the nodes are visited during an in-order traversal of the tree is

asked in DS by Veteran (106k points)
edited by | 2.7k views
@Arjun sir:

where to find such questions? where should i read this from?

and in exam if such questions, wont it be better not to attempt unless we know the exact algorithm?
Option D will be correct (even if S is considered to be the left child of Q)

.I am getting the option D as the answer . plz explain

( If W is the left child of U,then it cud be the answer??)
QSPTRUWV, please correct me if i am wrong.

5 Answers

+33 votes
Best answer

(A)  the inorder traversal order of a ternary tree is left $\rightarrow $ root $\rightarrow $ middle $\rightarrow $ right.

answered by Boss (19.7k points)
edited by
S is middle subtree of Q or left subtree..?? As per the answer they have assume it as left subtree.. why?? visually it seems middle subtree.. so middle subtree can not come with empty left subtree?? is this rule??
IDK about that rule... But then looking at the options it clear that it must be a left child only.
what is the inorder traversal of a 4-ary tree?
@Susgmita It will depend on how tree is and which type of sequence is given. If it's having numerical sequence then it will be a sorted array in ascending order
+8 votes

The inorder traversal of a ternary tree is given by Left > Root > Middle > Right.

But if you apply this traversal sequence on this tree, the order is SQPTWURV.

According to the answer given by various books, the answer is (A).

(A) can only be the answer if we consider 'S' to be the left child of 'Q', and 'W' to be the left child of 'U'.

answered by Loyal (9.8k points)
Here order will not start from S as well. It will start from Q because S is the middle element which will come after root element Q.
+6 votes
For inorder traversal you can take a trick
whenever you visit the node second time take it into inorder sequence.. :D
answered by Junior (891 points)
Is there such trick for other traversals as well, they may come in handy when solving for n-ary tree. @ankit srivastava


Preorder: When you visit the node the first time we can print the node

In order: When you visit the node the second time we can print the node

Postorder: When you visit the node the third time we can print the node



                            /         \

                        /                \

                     G                   H

@Lakshman Patel RJIT : for post order it is not clear, how do we visit G & H third time, we could only visit second time, can yiu explain ??


Yes,see this

ya, thanx
+3 votes
Inorder transversal of ternary tree is :- left -> Root->Middle->Right

from the given figure, it is not clear whether W is middle child of U or left child of U

if W is middle child of U then  SQPTRUWV (option D)

if W is left child of U then       SQPTRWUV  (option A)
answered by (197 points)
0 votes
answered by (19 points)
Answer is option A). You are doing some mistake. Please check one more time.

Related questions

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

42,599 questions
48,599 answers
63,729 users