0% found this document useful (0 votes)
51 views5 pages

Postorder Traversal Questions and Solutions

Uploaded by

tinomudadhakwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Postorder Traversal Questions and Solutions

Uploaded by

tinomudadhakwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Postorder Traversal: Questions and Solutions

Postorder Traversal - Questions and Full Solutions

Q1. 1. Define postorder traversal. Explain how it differs from preorder and inorder traversals.

1. Define postorder traversal. Explain how it differs from preorder and inorder traversals.

Solution:

Postorder traversal visits the nodes in the order: Left subtree -> Right subtree -> Root. In contrast:

- Preorder: Root -> Left -> Right

- Inorder: Left -> Root -> Right

So postorder is useful when you need to evaluate or delete trees since it ensures children are processed

before their parent.

Q2. 2. Given a binary tree, what is the order of visiting nodes in postorder traversal?

2. Given a binary tree, what is the order of visiting nodes in postorder traversal?

Solution:

You start with the left subtree, then move to the right subtree, and finally process the root node. This means

every node is processed after its subtrees.

Q3. 3. What is the time complexity of performing postorder traversal on a binary tree with n nodes?

3. What is the time complexity of performing postorder traversal on a binary tree with n nodes?

Solution:

The time complexity is O(n), because every node is visited exactly once.

Q4. 4. List the nodes in the order they are visited during postorder traversal of the tree:

4. List the nodes in the order they are visited during postorder traversal of the tree:
Postorder Traversal: Questions and Solutions

/\

B C

/\ \

D E F

Solution:

Traversal: D, E, B, F, C, A

Q5. 5. Write the pseudocode or recursive function for postorder traversal.

5. Write the pseudocode or recursive function for postorder traversal.

Solution:

Recursive pseudocode:

function postorder(node):

if node is not null:

postorder(node.left)

postorder(node.right)

visit(node)

Python version:

def postorder(node):

if node:

postorder(node.left)

postorder(node.right)

print(node.val)

Q6. 6. If node F was removed from the tree above, what would be the new postorder traversal seque

6. If node F was removed from the tree above, what would be the new postorder traversal sequence?
Postorder Traversal: Questions and Solutions

Solution:

Without F, the tree becomes:

/\

B C

/\

D E

New postorder: D, E, B, C, A

Q7. 7. Given the list representation tree = [1, 2, 3, None, 5, 6, 7], write a function to do postorder trav

7. Given the list representation tree = [1, 2, 3, None, 5, 6, 7], write a function to do postorder traversal.

Solution:

You can convert the list into a binary tree, then traverse:

class TreeNode:

def __init__(self, val):

self.val = val

self.left = self.right = None

def postorder(node):

if node:

postorder(node.left)

postorder(node.right)

print(node.val)

Q8. 8. What would be the output of the function below?

8. What would be the output of the function below?


Postorder Traversal: Questions and Solutions

def postorder(node):

if node:

postorder(node.left)

postorder(node.right)

print(node.val)

Solution:

It will print the values of all nodes in postorder order: left subtree -> right subtree -> node.

Q9. 9. Given inorder [D, B, E, A, C, F] and postorder [D, E, B, F, C, A], reconstruct the tree.

9. Given inorder [D, B, E, A, C, F] and postorder [D, E, B, F, C, A], reconstruct the tree.

Solution:

From postorder, the root is A. From inorder, left of A is [D, B, E], and right is [C, F].

- Left subtree (inorder: D, B, E | postorder: D, E, B): B is root

- Right subtree (inorder: C, F | postorder: F, C): C is root

Final tree:

/\

B C

/\ \

D E F

Q10. 10. Explain how postorder traversal is useful in deleting a binary tree.

10. Explain how postorder traversal is useful in deleting a binary tree.

Solution:

Postorder ensures you visit children before the parent. When deleting a tree, you must delete child nodes
Postorder Traversal: Questions and Solutions

before deleting their parent - making postorder the ideal traversal.

You might also like