📖 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 special order: first the left child, then the person themselves, and finally the right child. This order is called inorder traversal.
🎯 Goal: Build a small binary tree and write code to print the names of the people in the tree using inorder traversal (left child, root, right child).
📋 What You'll Learn
Create a class called Node with a String field name and two Node fields left and right.
Create a tree with three nodes: "Grandparent" as root, "Parent" as left child, and "Child" as right child.
Write a method called inorderTraversal that visits nodes in left-root-right order and prints their names.
Call inorderTraversal on the root node and print the names in order.
💡 Why This Matters
🌍 Real World
Tree traversal is used in many real-world 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 the Node class and tree nodes
Create a class called Node with a String field name and two Node fields called left and right. Then create three Node objects named grandparent, parent, and child with names "Grandparent", "Parent", and "Child" respectively.
DSA Java
Hint
Define the Node class with the fields and constructor. Then create the three nodes with the exact names and values.
2
Link the nodes to form the tree
Set the left child of grandparent to parent and the right child of grandparent to child.
DSA Java
Hint
Use the dot operator to set grandparent.left and grandparent.right to the correct nodes.
3
Write the inorder traversal method
Write a static method called inorderTraversal that takes a Node parameter called node. It should visit the left child by calling itself recursively, then print the name of the current node, then visit the right child by calling itself recursively. Add a check to return immediately if node is null.
DSA Java
Hint
Use recursion to visit left child, print current node's name, then visit right child. Return if node is null.
4
Call inorderTraversal and print the result
In the main method, call inorderTraversal with grandparent as the argument to print the names of the nodes in inorder sequence.
DSA Java
Hint
Call inorderTraversal(grandparent); to print the names in left-root-right order.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
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, 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 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 Java method signatures correctly represents an inorder traversal of a binary tree node named root?
easy
A. void inorderTraversal(Node root) { System.out.print(root.data); inorderTraversal(root.left); inorderTraversal(root.right); }
B. It visits nodes in postorder instead of inorder
C. It visits nodes in reverse inorder (Right, Root, Left)
D. It causes a NullPointerException
Solution
Step 1: Analyze the recursive call order
The method calls inorder on right subtree first, then prints root, then calls inorder on left subtree.
Step 2: Compare with inorder traversal definition
Standard inorder is Left, Root, Right. This code visits Right, Root, Left, which is reverse inorder.
Final Answer:
Visits nodes in reverse inorder (Right, Root, Left) -> Option C
Quick Check:
Right, Root, Left order detected [OK]
Hint: Check order of recursive calls to find traversal type [OK]
Common Mistakes:
Assuming it is correct inorder
Ignoring order of recursive calls
Confusing traversal types
5. You have a binary tree where each node contains a unique integer. You want to print all node values in ascending order using inorder traversal. However, the tree is not a binary search tree (BST). Which approach ensures correct ascending order output?
hard
A. Perform inorder traversal directly and print values as visited
B. Perform inorder traversal and then sort the collected values before printing
C. Perform preorder traversal and print values as visited
D. Perform postorder traversal and print values as visited
Solution
Step 1: Understand inorder traversal on non-BST
Inorder traversal visits nodes in Left, Root, Right order but does not guarantee sorted order if tree is not BST.
Step 2: Ensure ascending order output
To get ascending order, collect all node values during traversal, then sort them before printing.
Final Answer:
Perform inorder traversal and then sort the collected values before printing -> Option B
Quick Check:
Non-BST inorder not sorted; sorting needed [OK]
Hint: Inorder only sorts BST; sort after traversal for non-BST [OK]
Common Mistakes:
Assuming inorder traversal always sorts
Printing values during traversal without sorting
Using preorder or postorder expecting sorted output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.