📖 Scenario: Imagine you have a family tree stored as a simple tree structure. You want to visit each family member in a special order: first visit the left child, then the parent, then the right child. This is called inorder traversal.
🎯 Goal: Build a small binary tree and write code to print the values of the nodes in inorder (left, root, right) order.
📋 What You'll Learn
Create a simple binary tree with three nodes: root, left child, and right child
Add a helper variable to hold the result of the traversal
Write a recursive function to perform inorder traversal (left, root, right)
Print the list of node values after traversal
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like searching family trees, organizing files, and parsing expressions.
💼 Career
Understanding tree traversal is important for software developers working with data structures, databases, and algorithms.
Progress0 / 4 steps
1
Create a simple binary tree
Create a class called Node with an __init__ method that takes value, left, and right parameters. Then create three nodes: root with value 1, root.left with value 2, and root.right with value 3.
DSA Python
Hint
Define a class with __init__ to store value and left/right children. Then create three nodes as described.
2
Create a list to hold traversal results
Create an empty list called result to store the values of nodes visited during inorder traversal.
DSA Python
Hint
Just create an empty list named result to store traversal output.
3
Write the inorder traversal function
Write a recursive function called inorder that takes a node parameter. If the node is not None, first call inorder on node.left, then append node.value to result, then call inorder on node.right. Finally, call inorder(root) to start traversal.
DSA Python
Hint
Use recursion to visit left child, then current node, then right child. Append values to result.
4
Print the inorder traversal result
Write a print statement to display the result list after inorder traversal.
DSA Python
Hint
Print the result list to see the inorder traversal output.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Root, Left subtree, Right subtree
B. Root, Right subtree, Left subtree
C. Right subtree, Root, Left subtree
D. Left subtree, Root, Right subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits nodes in the order: left subtree first, then root node, then right subtree.
Step 2: Apply the order to the tree nodes
This means we first explore all nodes in the left subtree, then the root, then the right subtree.
Final Answer:
Left subtree, Root, Right subtree -> Option D
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder means Left, Root, then Right [OK]
Common Mistakes:
Confusing inorder with preorder or postorder
Visiting root before left subtree
Visiting right subtree before root
2. Which of the following Python function definitions correctly implements inorder traversal of a binary tree node root?
easy
A. def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right)
B. def inorder(root):
if root:
print(root.val)
inorder(root.left)
inorder(root.right)
C. def inorder(root):
if root:
inorder(root.right)
print(root.val)
inorder(root.left)
D. def inorder(root):
if root:
inorder(root.right)
inorder(root.left)
print(root.val)
Solution
Step 1: Recall inorder traversal order
Inorder visits left subtree, then root, then right subtree.
Step 2: Match code to order
def inorder(root):
if root:
inorder(root.left)
print(root.val)
inorder(root.right) calls inorder on left, prints root value, then calls inorder on right, matching inorder traversal.
Final Answer:
Option A code correctly implements inorder traversal -> Option A
Quick Check:
Left, Root, Right order in code [OK]
Hint: Inorder: recursive left call, print root, recursive right call [OK]
Common Mistakes:
Printing root before left subtree call
Swapping left and right subtree calls
Missing base case check for None
3. Given the binary tree below, what is the output of inorder traversal?
C. The base case is missing; should check if root is None first
D. The function calls right subtree before printing root
Solution
Step 1: Analyze base case in inorder function
The function checks root.left and root.right but never checks if root itself is None, which causes errors or no output if root is None.
Step 2: Identify missing base case effect
Without checking if root is None, the function may fail or skip printing when called with None, so adding if root is None: return fixes it.
Final Answer:
The base case is missing; should check if root is None first -> Option C
Quick Check:
Check root None before recursion [OK]
Hint: Always check if node is None before recursion [OK]
Common Mistakes:
Not checking if root is None before accessing attributes
Incorrect order of print and recursive calls
Forgetting to create root node before traversal
5. You have a binary tree where some nodes have only one child. Which inorder traversal output correctly represents the tree below?
5
/ \
3 8
\ \
4 9
Options:
A) 3 5 4 8 9
B) 4 3 5 9 8
C) 3 4 5 8 9
D) 5 3 4 8 9
hard
A. 3 5 4 8 9
B. 3 4 5 8 9
C. 4 3 5 9 8
D. 5 3 4 8 9
Solution
Step 1: Perform inorder traversal on given tree
Start at root (5), visit left subtree: node 3 has no left child, so print 3, then visit right child 4, print 4. Then print root 5. Then visit right subtree: node 8 has no left child, print 8, then right child 9, print 9.
Step 2: Write the output sequence
The output is: 3 4 5 8 9.
Final Answer:
3 4 5 8 9 -> Option B
Quick Check:
Inorder visits left, root, right even if child missing [OK]
Hint: Inorder visits left subtree fully before root, even if missing nodes [OK]
Common Mistakes:
Skipping nodes with only one child
Printing root before left subtree
Confusing preorder or postorder output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.