📖 Scenario: You are working with a simple family tree where each person has a left child and a right child. You want to visit each person in a specific order: first the left child, then the person themselves, and finally the right child. This order is called inorder traversal.
🎯 Goal: Build a program that creates a small binary tree, sets up a function to traverse it inorder (left, root, right), and prints the values of the nodes in that order.
📋 What You'll Learn
Create a binary tree node structure with an integer value and pointers to left and right children
Create a small binary tree with exactly 3 nodes: root with value 1, left child with value 2, right child with value 3
Write a recursive function called inorderTraversal that visits nodes in left-root-right order
Print the node values during the inorder traversal separated by spaces
💡 Why This Matters
🌍 Real World
Tree traversal is used in many applications like searching files, organizing data, and parsing expressions.
💼 Career
Understanding tree traversal is essential for software developers working with data structures, databases, and algorithms.
Progress0 / 4 steps
1
Create the binary tree nodes
Create a struct called Node with an int data field and two pointers called left and right to Node. Then create three nodes called root, leftChild, and rightChild with values 1, 2, and 3 respectively. Connect root.left to leftChild and root.right to rightChild.
DSA C
Hint
Use a struct with int data and two pointers left and right. Initialize nodes with values and connect them by assigning pointers.
2
Add the inorder traversal function declaration
Add a function declaration for void inorderTraversal(Node* node) above main. This function will later be used to visit nodes in left-root-right order.
DSA C
Hint
Write the function prototype with return type void and parameter Node* node.
3
Implement the inorder traversal function
Implement the inorderTraversal function to recursively visit the left child, print the current node's data followed by a space, and then visit the right child. Use an if statement to check if the node is not NULL before visiting children.
DSA C
Hint
Use recursion to visit left child, print current node, then right child. Check for NULL to stop recursion.
4
Call inorder traversal and print the output
In main, call inorderTraversal with the address of root. Then print a newline character using printf("\n") to end the output cleanly.
DSA C
Hint
Call inorderTraversal(&root); and then printf("\n"); to print the traversal result and a newline.
Practice
(1/5)
1. What is the order of nodes visited in an inorder tree traversal?
easy
A. Left subtree, Root, Right subtree
B. Root, Left subtree, Right subtree
C. Right subtree, Root, Left subtree
D. Root, Right subtree, Left subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal visits nodes in the order: left child, root node, then right child.
Step 2: Apply the order to the tree
This means we first visit all nodes in the left subtree, then the root, then all nodes in the right subtree.
Final Answer:
Left subtree, Root, Right subtree -> Option A
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 C function prototypes correctly represents an inorder traversal of a binary tree node struct Node?
easy
A. int inorder(struct Node* root);
B. int inorder(struct Node root);
C. void inorder(struct Node root);
D. void inorder(struct Node* root);
Solution
Step 1: Identify correct parameter type for tree traversal
Traversal functions usually take a pointer to the root node to access the tree nodes.
Step 2: Check return type and parameter
Traversal functions typically return void as they print or process nodes during traversal, so void return type with pointer parameter is correct.
Final Answer:
void inorder(struct Node* root); -> Option D
Quick Check:
Traversal uses pointer and void return [OK]
Hint: Traversal functions use pointer to root and void return [OK]
Common Mistakes:
Passing struct by value instead of pointer
Using int return type instead of void
Missing pointer in parameter
3. Given the binary tree below:
2
/ \
1 3
What is the output of the inorder traversal function that prints node values?
medium
A. 2 1 3
B. 1 3 2
C. 1 2 3
D. 3 2 1
Solution
Step 1: Identify inorder traversal order
Inorder visits left subtree, then root, then right subtree.
Step 2: Apply inorder to given tree
Left child is 1, root is 2, right child is 3, so output is "1 2 3".
Final Answer:
1 2 3 -> Option C
Quick Check:
Inorder of 2-node tree = 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 code snippet for inorder traversal: