Binary Tree Traversals Java | Fresher Interview
Fast recall
Quick revision
Core answer
Whether you know all four traversals, which are DFS vs BFS, and the inorder-sorted-BST property
Mixing up the Root position in pre/in/post, or not knowing level-order needs a queue
State that inorder of a BST is sorted and that level-order is BFS via a queue
Interview-ready guide
Interview answer
A binary tree is a hierarchical structure where each node holds a value and has at most two children, a left child and a right child.
The topmost node is the root, and nodes with no children are leaves. There are four standard ways to traverse it. Three of them are depth-first, and they differ only in when you visit the root relative to its subtrees.
Preorder visits the root first, then the left subtree, then the right. Inorder visits the left subtree, then the root, then the right. Postorder visits the left subtree, then the right, then the root last.
The naming is a helpful mnemonic: pre, in, and post refer to where the root is visited, first, middle, or last. The fourth traversal is level-order, which is breadth-first. Instead of recursion, it uses a queue: you enqueue the root, then repeatedly dequeue a node, visit it, and enqueue its children, which processes the tree level by level from top to bottom.
A really important property I'd mention is that an inorder traversal of a binary search tree produces the values in sorted ascending order, which is a direct consequence of the BST ordering rule and comes up constantly in interviews. All four traversals are O of n time, since each visits every node exactly once.
The recursive DFS versions are concise, while level-order specifically needs a queue, and that distinction, recursion or stack gives DFS, queue gives BFS, is worth being crisp about.
Binary Tree Traversals Java | Fresher Interview
Direct Answer
A binary tree is a hierarchy where each node has at most two children (left and right). The four traversals: inorder (Left-Root-Right), preorder (Root-Left-Right), postorder (Left-Right-Root) are DFS (recursive/stack), and level-order is BFS (uses a queue). For a BST, inorder yields sorted order. All are O(n) time.
Deep Dive & Architecture
A binary tree node has at most two children. Traversal means visiting every node in a deliberate order. Depth-first traversals differ only in when the root is visited relative to its left and right subtrees.
For a tree with root 1, children 2 and 3, and children 4 and 5 under node 2:
- Preorder,
root-left-right, gives1, 2, 4, 5, 3. It is useful when a parent must be handled before its descendants. - Inorder,
left-root-right, gives4, 2, 5, 1, 3. It produces sorted values only when the tree is a Binary Search Tree. - Postorder,
left-right-root, gives4, 5, 2, 3, 1. It is useful when child results are needed before the parent, such as height or deletion. - Level-order gives
1, 2, 3, 4, 5. It uses a queue and visits by distance from the root.
Every traversal touches each node once, so time is O(n). Recursive DFS uses O(h) call-stack space for tree height h; a skewed tree can make that O(n). Level-order uses O(w) queue space for the tree's maximum width. An empty root produces an empty traversal.
Code Example
class Node { int val; Node left, right; Node(int v){ val = v; } }
void inorder(Node n) { // Left, Root, Right
if (n == null) return;
inorder(n.left);
System.out.print(n.val + " "); // visit root between children
inorder(n.right);
}
void levelOrder(Node root) { // BFS with a queue
if (root == null) return;
Queue<Node> q = new ArrayDeque<>();
q.offer(root);
while (!q.isEmpty()) {
Node n = q.poll();
System.out.print(n.val + " ");
if (n.left != null) q.offer(n.left);
if (n.right != null) q.offer(n.right);
}
}
// On a BST, inorder prints values in ascending sorted order.
Common Mistakes & Pitfalls
- Confusing pre/in/post: the name says where the Root is - pre=first, in=middle, post=last.
- Level-order without a queue: BFS needs a queue; recursion gives DFS, not BFS.
- Forgetting inorder-BST = sorted: a frequently tested property.
- No null check: recursion must stop at
nullchildren.
Check your understanding
Answer these without looking back at the article.
- What is a Binary Search Tree (BST) and what are its properties?
- How do you find the height of a binary tree recursively?
- How does level-order traversal (BFS) work on a binary tree?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.