Binary Tree Fundamentals
Binary Tree Fundamentals
This section covers foundational concepts of binary trees: types of trees and the three fundamental depth-first traversals. Understanding these concepts is essential for solving tree problems in software engineering interviews.
Types of Binary Trees
Three key terms describe binary trees based on node structure and filling pattern: full tree, complete binary tree, and perfect binary tree.
Full Tree
In a full binary tree, every node has either 0 or 2 children. No node has exactly one child.
Leaf nodes (nodes with no children) are allowed. Internal nodes that have children must have exactly two children.
The tree is "full" because whenever a node decides to have descendants, it must have two.
Complete Binary Tree
A complete binary tree is filled top to bottom, left to right. All levels are completely filled except possibly the last level, which is filled from left to right with no gaps.
If a node is inserted out of order (e.g., a node in the last level that is not as left as possible), the tree is no longer complete.
Complete binary trees are used to implement binary heaps.
Perfect Binary Tree
A perfect binary tree is both full and complete.
All leaf nodes are at the same level, and every internal node has exactly two children.
It satisfies the conditions of both full and complete trees simultaneously.
Binary Tree Traversals
Three fundamental depth-first traversals exist: pre-order, in-order, and post-order. The prefix (pre, in, post) indicates when the current node is visited relative to its left and right subtrees.
Key concept: In all traversals, the left subtree is always visited before the right subtree. The prefix alone changes when the node itself is visited.
Recursion is used to implement these traversals naturally. Each node's stack frame contains three tasks: visit left, visit node, visit right, in the order determined by the traversal. The call stack maintains the state for each recursive call. The same policy can be implemented iteratively using an explicit stack.
Pre-order Traversal
Policy: Visit node → left subtree → right subtree.
The node is visited before both of its subtrees. This traversal is often used to copy a tree or get a prefix expression.
Example walkthrough on a sample tree (root 10; left subtree: 7 → (6 → 1, 8 → 9); right subtree: 11 → (20 → (14, 22))):
Start at root (10): visit 10, go left.
At 7: visit 7, go left.
At 6: visit 6, go left.
At 1: visit 1, go left (null, return), go right (null, return), return to 6.
6: go right (null), return to 7.
7: go right to 8.
At 8: visit 8, go left (null), go right to 9.
At 9: visit 9, go left (null), go right (null), return to 8 (finished), return to 7 (finished), return to 10.
10: go right to 11.
At 11: visit 11, go left (null), go right to 20.
At 20: visit 20, go left to 14.
At 14: visit 14, go left (null), go right (null), return to 20.
20: go right to 22.
At 22: visit 22, go left (null), go right (null), return to 20 (finished), return to 11 (finished), return to 10 (finished).
Visited order: 10, 7, 6, 1, 8, 9, 11, 20, 14, 22.
In-order Traversal
Policy: Left subtree → visit node → right subtree.
The node is visited between its left and right subtrees. For a binary search tree (BST), in-order traversal yields nodes in sorted increasing order.
Example walkthrough (same tree):
Root 10: go left to 7.
7: go left to 6.
6: go left to 1.
1: go left (null), visit 1, go right (null), return to 6.
6: visit 6, go right (null), return to 7.
7: visit 7, go right to 8.
8: go left (null), visit 8, go right to 9.
9: go left (null), visit 9, go right (null), return to 8 (finished), return to 7 (finished), return to 10.
10: visit 10, go right to 11.
11: go left (null), visit 11, go right to 20.
20: go left to 14.
14: go left (null), visit 14, go right (null), return to 20.
20: visit 20, go right to 22.
22: go left (null), visit 22, go right (null), return to 20 (finished), return to 11 (finished), return to 10 (finished).
Visited order: 1, 6, 7, 8, 9, 10, 11, 14, 20, 22.
Note: For a binary search tree, in-order traversal visits nodes in non-decreasing order because all values in the left subtree are less than the node, and all values in the right subtree are greater.
Post-order Traversal
Policy: Left subtree → right subtree → visit node.
The node is visited after both of its subtrees. This traversal is used to delete a tree or evaluate postfix expressions.
Example walkthrough (same tree):
Root 10: go left to 7.
7: go left to 6.
6: go left to 1.
1: go left (null), go right (null), visit 1, return to 6.
6: go right (null), visit 6, return to 7.
7: go right to 8.
8: go left (null), go right to 9.
9: go left (null), go right (null), visit 9, return to 8.
8: visit 8, return to 7.
7: visit 7, return to 10.
10: go right to 11.
11: go left (null), go right to 20.
20: go left to 14.
14: go left (null), go right (null), visit 14, return to 20.
20: go right to 22.
22: go left (null), go right (null), visit 22, return to 20.
20: visit 20, return to 11.
11: visit 11, return to 10.
10: visit 10.
Visited order: 1, 6, 9, 8, 7, 14, 22, 20, 11, 10.
Complexity and Additional Notes
All three traversals visit each node exactly once, so time complexity is O(n), where n is the number of nodes.
Recursive implementations use the call stack implicitly. An explicit stack can also be used for iterative traversal, with the order of pushes adjusted to achieve the desired traversal.
These traversals are depth-first; a breadth-first traversal (level-order) uses a queue and visits nodes level by level from top to bottom.
Variations like zigzag traversal (alternating left-to-right and right-to-left per level) build on the same fundamental concepts.
Understanding these foundational traversals allows flexible adaptation to different tree problems, such as computing depths, validating BST properties, collecting pattern-specific values, or converting trees to other structures.

Ask anything
We've read this whole study set, so questions about it get the best answers.