# Binary Tree Traversals

> Traverse trees with preorder, inorder, postorder and level order, both recursively and iteratively, and compute basic properties like height. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/dsa-placement/binary-trees
Last updated: 2026-11-28

Traverse trees with preorder, inorder, postorder and level order, both recursively and iteratively, and compute basic properties like height.

Beginner · about 7 hours · difficulty 2/5

Learn first: [Backtracking](https://www.internhack.in/roadmaps/tracks/dsa-placement/backtracking), [Stacks and Queues](https://www.internhack.in/roadmaps/tracks/dsa-placement/stacks-queues)

Trees are recursion in disguise. Almost every tree problem is a traversal plus a small piece of logic at each node.

### Traversals

- **DFS orders**: preorder (node, left, right), inorder (left, node, right), postorder (left, right, node).
- **Iterative versions** with an explicit stack. Interviewers often ask for iterative inorder.
- **Level order (BFS)** with a queue, processing one level at a time by reading the queue size.
- **Morris traversal**: O(1) extra space inorder, an occasional follow-up.

### Basic tree recursion

Height (Maximum Depth of Binary Tree), count nodes, Same Tree, Symmetric Tree, Invert Binary Tree, Path Sum. Write each as: handle null, recurse on children, combine.

### Building trees

Construct Binary Tree from Preorder and Inorder Traversal uses a hash map from value to inorder index. Know why preorder plus inorder (or postorder plus inorder) is enough but preorder plus postorder is not, in general.

### Classic problems

Binary Tree Inorder Traversal (iterative), Level Order Traversal, Zigzag Level Order, Maximum Depth, Same Tree, Symmetric Tree, Invert Binary Tree, Path Sum, Construct Binary Tree from Preorder and Inorder.

### Pitfalls

- Forgetting the null base case.
- Mixing up height (edges vs nodes) definitions; ask the interviewer which one they mean.

You are ready to move on when you can write all four traversals iteratively and explain the level-size trick in BFS.

## Key concepts

- **Tree terminology**: Root, leaf, depth, height
- **Preorder, inorder, postorder**
- **Iterative traversals with a stack**
- **Level order with a queue**
- **Construct tree from traversals**
- **Morris traversal**

## Mini project

Solve these 10 in 7 days, log time and pattern: Binary Tree Inorder Traversal (iterative), Preorder (iterative), Postorder (iterative), Level Order Traversal, Zigzag Level Order Traversal, Maximum Depth, Same Tree, Symmetric Tree, Invert Binary Tree, Construct Binary Tree from Preorder and Inorder Traversal.

## Check yourself

How does BFS process exactly one level per outer loop iteration?

## Free resources

- [VisuAlgo: binary trees and BST](https://visualgo.net/en/bst) (VisuAlgo)
- [takeUforward tree series](https://takeuforward.org/) (takeUforward)
- [NeetCode roadmap: Trees](https://neetcode.io/roadmap) (NeetCode)

Previous: [Monotonic Stack and Deque](https://www.internhack.in/roadmaps/tracks/dsa-placement/monotonic-stack) · Next: [Tree Problem Patterns](https://www.internhack.in/roadmaps/tracks/dsa-placement/tree-patterns)

## More in Trees, BSTs and Heaps

- [Binary Tree Traversals](https://www.internhack.in/roadmaps/tracks/dsa-placement/binary-trees)
- [Tree Problem Patterns](https://www.internhack.in/roadmaps/tracks/dsa-placement/tree-patterns)
- [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/dsa-placement/bst)
- [Heaps and Priority Queues](https://www.internhack.in/roadmaps/tracks/dsa-placement/heaps)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DSA and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/dsa-placement)
