ReviseAlgo Logo
EasyVery High Frequency25 min

Binary Tree Fundamentals

Tree terminology, traversal orders (in/pre/post/level), and recursive thinking.

Binary tree = each node has at most 2 children. Master pre/in/post-order DFS and level-order BFS. Most tree problems are solved with recursion.

Binary Tree Fundamentals

Operation Complexities

Operation
Time
Note
Pre/In/Post-order traversal
O(n)
Visit every node once
Height calculation
O(n)
Post-order: 1 + max(leftH, rightH)
Search in BST
O(h)
O(log n) balanced, O(n) skewed
Insert in BST
O(h)
Find correct leaf position
Level-order (BFS)
O(n)
Queue-based, visits by level

Code

Tree Traversals

O(n)All three traversals have same O(n) time complexity
Step-by-step1 / 11
Treelevel links
root12345
1. 1

Tree: 1 is root

Root=1, left subtree rooted at 2 (with children 4,5), right subtree rooted at 3.