Inorder traversal visits nodes in this order: left child, root node, then right child. It means we first go to the left subtree, then visit the root, and finally the right subtree.
Click to reveal answer
beginner
Why is inorder traversal useful for binary search trees (BST)?
Inorder traversal of a BST visits nodes in ascending order. This helps to get sorted data from the tree easily.
Click to reveal answer
beginner
What is the order of visiting nodes in inorder traversal?
The order is: Left subtree → Root node → Right subtree.
Click to reveal answer
beginner
Show the inorder traversal output for this tree: Root=2, Left=1, Right=3
The inorder traversal visits nodes as: 1 → 2 → 3
Click to reveal answer
beginner
What is the base case in recursive inorder traversal?
The base case is when the current node is null (no node). Then, we stop and return.
Click to reveal answer
In inorder traversal, which node do we visit first?
ARight child
BLeft child
CRoot node
DNone of the above
✗ Incorrect
In inorder traversal, we always visit the left child first.
What is the inorder traversal output of a BST with nodes 2 (root), 1 (left), 3 (right)?
A1 → 2 → 3
B2 → 1 → 3
C3 → 2 → 1
D1 → 3 → 2
✗ Incorrect
Inorder traversal visits left, root, right, so output is 1 → 2 → 3.
Which traversal gives nodes in ascending order for a BST?
APreorder
BPostorder
CInorder
DLevel order
✗ Incorrect
Inorder traversal visits BST nodes in ascending order.
What should the recursive function do when it reaches a null node in inorder traversal?
AReturn immediately
BGo to left child
CGo to right child
DVisit the node
✗ Incorrect
When node is null, recursion returns immediately (base case).
In inorder traversal, after visiting the left subtree, what is visited next?
ARight subtree
BNone
CLeft subtree again
DRoot node
✗ Incorrect
After left subtree, we visit the root node.
Explain how inorder traversal works on a binary tree.
Think about the order of visiting nodes and how recursion helps.
You got /5 concepts.
Why is inorder traversal important for binary search trees?
Consider the BST property and how inorder visits nodes.
You got /4 concepts.
Practice
(1/5)
1. What is the order of nodes visited in an inorder traversal of a binary tree?
easy
A. Root node, Right subtree, Left subtree
B. Root node, Left subtree, Right subtree
C. Right subtree, Root node, Left subtree
D. Left subtree, Root node, Right subtree
Solution
Step 1: Understand inorder traversal definition
Inorder traversal means visiting the left subtree first, then the root node, and finally the right subtree.
Step 2: Apply the traversal order
Following this order ensures nodes are visited in ascending order for binary search trees.
Final Answer:
Left subtree, Root node, Right subtree -> Option D
Quick Check:
Inorder = Left, Root, Right [OK]
Hint: Remember: Inorder = Left, Root, 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 Go function signatures correctly represents an inorder traversal function for a binary tree node defined as type Node struct { Val int; Left, Right *Node }?
easy
A. func inorder(root *Node) []int
B. func inorder(root Node) []int
C. func inorder(root *Node) int
D. func inorder(root Node) int
Solution
Step 1: Understand pointer usage in Go for trees
Binary trees use pointers to nodes to allow recursive traversal and modification.
Step 2: Choose function signature returning slice of ints
Inorder traversal returns a list of node values, so return type should be []int, and input should be pointer to Node.
Final Answer:
func inorder(root *Node) []int -> Option A
Quick Check:
Pointer receiver and slice return type [OK]
Hint: Use pointer receiver and return slice for traversal [OK]
Common Mistakes:
Passing Node by value instead of pointer
Returning single int instead of slice
Using incorrect return type
3. What is the output of the inorder traversal for the following binary tree?
2
/ \
1 3
Given the Go code:
type Node struct {
Val int
Left, Right *Node
}
func inorder(root *Node) []int {
if root == nil {
return []int{}
}
left := inorder(root.Left)
right := inorder(root.Right)
return append(append(left, root.Val), right...)
}
func main() {
root := &Node{2, &Node{1, nil, nil}, &Node{3, nil, nil}}
result := inorder(root)
fmt.Println(result)
}
medium
A. [3, 2, 1]
B. [2, 1, 3]
C. [1, 2, 3]
D. [1, 3, 2]
Solution
Step 1: Trace inorder traversal on given tree
Visit left subtree (1), then root (2), then right subtree (3).
Step 2: Confirm output list order
The traversal returns [1, 2, 3] as expected for inorder.
Final Answer:
[1, 2, 3] -> Option C
Quick Check:
Inorder traversal output = [1, 2, 3] [OK]
Hint: Inorder visits left, root, then right [OK]
Common Mistakes:
Appending root before left subtree
Appending right subtree before root
Confusing preorder or postorder output
4. The following Go code attempts inorder traversal but produces incorrect output. What is the bug?
func inorder(root *Node) []int {
if root == nil {
return []int{}
}
left := inorder(root.Left)
right := inorder(root.Right)
return append(left, right...)
return append(result, root.Val)
}
medium
A. The root value is appended after the return statement, so never executed
B. The function should return int, not []int
C. The base case should return nil, not empty slice
D. The left subtree is not traversed
Solution
Step 1: Analyze return statements in function
The first return statement returns append(left, right...), so the second return is unreachable.
Step 2: Identify missing root value append
Root value is never appended because code after first return is ignored.
Final Answer:
The root value is appended after the return statement, so never executed -> Option A
Quick Check:
Code after return is unreachable [OK]
Hint: Check for unreachable code after return [OK]
Common Mistakes:
Ignoring unreachable code after return
Not appending root value in correct order
Confusing base case return value
5. Given a binary tree where some nodes have only one child, how does inorder traversal help in printing the nodes in sorted order if the tree is a binary search tree? Consider the tree:
5
/ \
3 8
\ \
4 10
What is the inorder traversal output?
hard
A. [5, 3, 4, 8, 10]
B. [3, 4, 5, 8, 10]
C. [4, 3, 5, 10, 8]
D. [3, 5, 4, 8, 10]
Solution
Step 1: Apply inorder traversal on given tree
Visit left subtree: node 3, then its right child 4, then root 5, then right subtree nodes 8 and 10.
Step 2: List nodes in inorder sequence
Sequence is 3, 4, 5, 8, 10 which is sorted ascending order.
Final Answer:
[3, 4, 5, 8, 10] -> Option B
Quick Check:
Inorder traversal of BST gives sorted nodes [OK]
Hint: Inorder traversal of BST prints nodes sorted [OK]
Common Mistakes:
Ignoring nodes with single child
Visiting root before left subtree
Mixing preorder or postorder with inorder
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.