Challenge - 5 Problems
Inorder Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of inorder traversal?
Given the binary tree below, what is the output of inorder traversal (Left, Root, Right)?
Tree structure:
10
/ \
5 15
/ \ \
3 7 20
Tree structure:
10
/ \
5 15
/ \ \
3 7 20
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Node root = new Node(10); root.left = new Node(5); root.right = new Node(15); root.left.left = new Node(3); root.left.right = new Node(7); root.right.right = new Node(20); inorder(root); } }
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree first, then root, then right subtree.
✗ Incorrect
Inorder traversal visits nodes in the order: left child, root, right child. So for this tree, the order is 3 (leftmost), 5 (parent of 3 and 7), 7 (right child of 5), 10 (root), 15 (right child of 10), 20 (right child of 15).
❓ Predict Output
intermediateInorder traversal output for a skewed tree
What is the output of inorder traversal for the following right-skewed binary tree?
Tree structure:
1
\
2
\
3
\
4
Tree structure:
1
\
2
\
3
\
4
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class Main { public static void inorder(Node root) { if (root == null) return; inorder(root.left); System.out.print(root.val + " "); inorder(root.right); } public static void main(String[] args) { Node root = new Node(1); root.right = new Node(2); root.right.right = new Node(3); root.right.right.right = new Node(4); inorder(root); } }
Attempts:
2 left
💡 Hint
Inorder visits left first, but here left nodes are null.
✗ Incorrect
Since all left children are null, inorder traversal visits nodes in ascending order of their values: 1, 2, 3, 4.
🔧 Debug
advancedIdentify the error in inorder traversal code
What error will this code produce when performing inorder traversal on a binary tree?
DSA Java
public void inorder(Node root) {
if (root == null) return;
inorder(root.right);
System.out.print(root.val + " ");
inorder(root.left);
}Attempts:
2 left
💡 Hint
Check the order of recursive calls for left and right children.
✗ Incorrect
The code visits right subtree first, then root, then left subtree, which is reverse inorder traversal. It will print nodes in descending order instead of ascending.
🧠 Conceptual
advancedInorder traversal property of BST
Which statement about inorder traversal of a Binary Search Tree (BST) is true?
Attempts:
2 left
💡 Hint
Think about how BST stores smaller values on left and larger on right.
✗ Incorrect
Inorder traversal visits left subtree, root, then right subtree. For BST, this results in nodes being printed in ascending order.
🚀 Application
expertNumber of nodes visited before root in inorder traversal
In an inorder traversal of a binary tree, how many nodes are visited before the root node is printed?
Attempts:
2 left
💡 Hint
Inorder visits left subtree fully before root.
✗ Incorrect
In inorder traversal, the entire left subtree is visited before the root node is printed.
