Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Given Pre-Order Traversal and Post-Order Traversal of a binary tree draw the tree

This is a sample question in Syllabus of Computer Officer Exam of Public Service Commission (Lok Sewa Ayog). The pre-order and post-order traversal of a binary tree is given as below, obtain the binary tree that resemble with traversal. Preorder : u1 u2 u3 u4 u10 u8 u5 u9 u6 u11 u7 Postorder: u4 u10 u3 u8 u2 u9 u11 u7 u6 u5 u1 Answer: Following is the binary tree that resembles with the given traversal orders:

Procedure: Because U1 is the first node in preorder traversal, it is the root of tree. Draw the root node. Look at the second node in preorder which is u2 and because u2 is before u1 in post order traversal, it is the child of u1 node. Draw u2 node. Look at the third node u3 on preorder which is before u2 in post order. So, u3 the child node of u2. Draw u3 node. Take fourth node u4 in pre-order. This is before u3 in post-order list. So this is the child of u3. Draw u4 as child node of u3.

Look at the fifth node u10 in pre-order list. u10 is not before u4, so it is not the child of u4. But it is before u3 in post order so it is the another child of u3. Draw u10 as right child of u3. Look at the sixth node u8 in pre-order traversal. u8 is not before u10 in post order, so it is not the child of u10. u8 is not before u4, so its not the child of u4. Its not even before u3. Its not the child of u3 too! But it is before u2 in post order traversal, So u8 is another child of u2. Draw u8 as right child of u2. Look at the seventh node u5 in preorder traversal. u5 is after u8, u10, u4, u3, u2 but it is before u1. So It is the right child of u1. Draw u5. Look at the eighth node u9. It is before u5. So it is the child of u5. Draw u9. Look at ninth node u6. It is before u5. So it is another child of u6. Draw u6. Look at tenth node u11. It is before u6. So it is the child of u6. Draw u11 as child of u6. Look at the last node u7. It is before u6 in post order traversal. So, it is another child of u6. This way you constructed the binary tree as shown in figure which resembles the given preorder and postorder traversals.

You might also like