📖 Scenario: Imagine you have a family tree stored in a simple tree structure. You want to visit each family member in a special order: first the left child, then the parent, then the right child. This is called inorder traversal.
🎯 Goal: You will build a small program that creates a simple tree, sets up a helper function to do inorder traversal, and then prints the nodes in the correct order.
📋 What You'll Learn
Create a simple binary tree with exactly three nodes: root with value 1, left child with value 2, right child with value 3
Create a helper function called inorderTraversal that takes a pointer to the root node and prints the values in inorder (left, root, right)
Call the inorderTraversal function with the root node
Print the values separated by spaces on one line
💡 Why This Matters
🌍 Real World
Tree traversals are used in many real-world applications like searching family trees, organizing files, and parsing expressions.
💼 Career
Understanding tree traversal is fundamental for software engineers working with data structures, databases, and algorithms.
Progress0 / 4 steps
1
Create the tree nodes
Create a struct called Node with an int data, and two pointers left and right. Then create three nodes: root with data 1, root->left with data 2, and root->right with data 3.
DSA C++
Hint
Start by defining a struct with data and two child pointers. Then create the root node and assign its left and right children.
2
Create the inorder traversal function
Write a function called inorderTraversal that takes a Node* called root. It should recursively visit the left child, print the root's data followed by a space, then visit the right child.
DSA C++
Hint
Use recursion: first call the function on the left child, then print the current node's data, then call the function on the right child.
3
Call the inorder traversal function
Call the inorderTraversal function with the variable root to start the traversal.
DSA C++
Hint
Simply call the function with the root node to start the traversal.
4
Print the traversal output
Add a std::cout statement to print a newline after the inorder traversal finishes.
DSA C++
Hint
After printing all nodes, print a newline to end the output cleanly.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Right subtree, Root, Left subtree
B. Root, Left subtree, Right subtree
C. Left subtree, Root, Right subtree
D. Root, Right subtree, Left subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits nodes in the order: left child, then root, then right child.
Step 2: Apply to binary tree traversal
This means we first visit all nodes in the left subtree, then the root node, then all nodes in the right subtree.
Final Answer:
Left subtree, Root, Right subtree -> Option C
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 is the correct C++ function signature for inorder traversal of a binary tree node pointer Node* root?
easy
A. int inorderTraversal(Node* root)
B. int inorderTraversal(Node root)
C. void inorderTraversal(Node root)
D. void inorderTraversal(Node* root)
Solution
Step 1: Identify correct parameter type
Traversal functions usually take a pointer to the root node to access the tree nodes.
Step 2: Identify correct return type
Traversal functions typically return void because they print or process nodes without returning a value.
Final Answer:
void inorderTraversal(Node* root) -> Option D
Quick Check:
Pointer parameter and void return [OK]
Hint: Traversal functions use Node* and void return [OK]
Common Mistakes:
Passing node by value instead of pointer
Using int return type instead of void
Missing pointer symbol * in parameter
3. Given the binary tree:
2
/ \
1 3
What is the output of the inorder traversal function that prints node values separated by spaces?
medium
A. 1 2 3
B. 2 1 3
C. 3 2 1
D. 1 3 2
Solution
Step 1: Traverse left subtree
Start at root (2), go left to node (1), print 1 since it has no left child.
Step 2: Visit root and right subtree
Return to root (2), print 2, then go right to node (3), print 3.
Final Answer:
1 2 3 -> Option A
Quick Check:
Inorder traversal output = 1 2 3 [OK]
Hint: Inorder prints nodes in ascending order for BST [OK]
Common Mistakes:
Printing root before left subtree
Printing right subtree before root
Confusing preorder or postorder output
4. Consider this C++ inorder traversal code snippet: