# Trees, BSTs & Graph Traversal in C++

> Binary trees, binary search trees, DFS and BFS in C++: concepts, interactive traversals, interview questions and university exam preparation.

- Canonical URL: https://shafinchowdhury.dev/topics/trees-graphs/
- Author: Shafin Chowdhury (https://shafinchowdhury.dev/about/)

Trees and graphs are where data structures stop being linear. This series starts with tree vocabulary and the four traversals, adds the binary search tree's ordering rule, drills the interview and university exam questions built on both, and ends with depth-first and breadth-first search on general graphs. Every article uses the same example structures, tested C++ code and step-by-step diagrams.

## Curated Learning Path

1. **[Beginner]** [Trees, Binary Trees & Traversals](https://shafinchowdhury.dev/posts/tree-binary-tree-traversals/) ([Markdown](https://shafinchowdhury.dev/posts/tree-binary-tree-traversals.md)) — Terminology, full vs complete vs perfect trees, building a tree, and inorder, preorder, postorder and level order.
2. **[Beginner]** [Binary Search Tree (BST)](https://shafinchowdhury.dev/posts/binary-search-tree-bst/) ([Markdown](https://shafinchowdhury.dev/posts/binary-search-tree-bst.md)) — The BST property, search, insertion, the three deletion cases, successor, validation and balance.
3. **[Intermediate]** [Tree Interview Questions](https://shafinchowdhury.dev/posts/tree-interview-questions/) ([Markdown](https://shafinchowdhury.dev/posts/tree-interview-questions.md)) — Height, diameter, LCA, symmetric and balanced checks, building from traversals, serialization.
4. **[Intermediate]** [BST Interview Questions](https://shafinchowdhury.dev/posts/bst-interview-questions/) ([Markdown](https://shafinchowdhury.dev/posts/bst-interview-questions.md)) — Kth smallest, LCA in a BST, floor and ceil, recover a BST, BST to linked list, largest BST subtree.
5. **[Beginner to Intermediate]** [Tree Questions for University Exams](https://shafinchowdhury.dev/posts/tree-questions-university-exams-bangladesh/) ([Markdown](https://shafinchowdhury.dev/posts/tree-questions-university-exams-bangladesh.md)) — Written, lab and viva practice for CSE students in Bangladesh: traversals, reconstruction, iterative algorithms.
6. **[Beginner to Intermediate]** [BST Questions for University Exams](https://shafinchowdhury.dev/posts/bst-questions-university-exams-bangladesh/) ([Markdown](https://shafinchowdhury.dev/posts/bst-questions-university-exams-bangladesh.md)) — BST construction from a sequence, deletion traces, pseudocode, worst case and viva answers.
7. **[Beginner]** [What Are DFS and BFS?](https://shafinchowdhury.dev/posts/what-is-dfs-and-bfs/) ([Markdown](https://shafinchowdhury.dev/posts/what-is-dfs-and-bfs.md)) — A visual beginner's guide to graphs, stacks, queues and the two basic graph traversals.
8. **[Intermediate]** [DFS vs BFS](https://shafinchowdhury.dev/posts/dfs-vs-bfs/) ([Markdown](https://shafinchowdhury.dev/posts/dfs-vs-bfs.md)) — Side-by-side comparison with shortest paths, cycle detection, connected components and interview questions.

## Articles by Category

### 1. Trees & Binary Search Trees

Core concepts and C++ implementations.

- [Trees, Binary Trees & Tree Traversals in C++: Complete Beginner's Guide](https://shafinchowdhury.dev/posts/tree-binary-tree-traversals/): Binary tree in C++: terminology, full vs complete vs perfect trees, building a tree, and inorder, preorder, postorder and level order traversal. ([Markdown](https://shafinchowdhury.dev/posts/tree-binary-tree-traversals.md))
- [Binary Search Tree (BST) in C++: Search, Insert, Delete & Complete Guide](https://shafinchowdhury.dev/posts/binary-search-tree-bst/): Binary search tree in C++: the BST property, search, insert, all three deletion cases with diagrams, successor, validation and complexity, with tested code. ([Markdown](https://shafinchowdhury.dev/posts/binary-search-tree-bst.md))

### 2. Interview Preparation

The most common coding interview questions with tested C++.

- [Most Common Tree Interview Questions (With C++ Solutions)](https://shafinchowdhury.dev/posts/tree-interview-questions/): Tree interview questions from easy to hard: height, diameter, LCA, symmetric, balanced, build from traversals and serialize, with tested C++ solutions. ([Markdown](https://shafinchowdhury.dev/posts/tree-interview-questions.md))
- [Most Common Binary Search Tree (BST) Interview Questions (With C++ Solutions)](https://shafinchowdhury.dev/posts/bst-interview-questions/): 20 BST interview questions with tested C++: validate BST, kth smallest, LCA, floor and ceil, recover BST, BST to linked list and largest BST subtree. ([Markdown](https://shafinchowdhury.dev/posts/bst-interview-questions.md))

### 3. University Exam Preparation (Bangladesh)

Written, lab and viva questions with model answers.

- [Tree Questions for University Exams in Bangladesh: Written, Lab & Viva (With Answers)](https://shafinchowdhury.dev/posts/tree-questions-university-exams-bangladesh/): Tree questions for university exams in Bangladesh with model answers: traversals, building a tree from traversals, recursive and iterative algorithms, viva. ([Markdown](https://shafinchowdhury.dev/posts/tree-questions-university-exams-bangladesh.md))
- [Binary Search Tree (BST) Questions for University Exams in Bangladesh: Construction, Deletion & Viva (With Answers)](https://shafinchowdhury.dev/posts/bst-questions-university-exams-bangladesh/): BST exam questions for CSE students in Bangladesh: build a binary search tree from a sequence, traversals, all three deletion cases, pseudocode, viva answers. ([Markdown](https://shafinchowdhury.dev/posts/bst-questions-university-exams-bangladesh.md))

### 4. Graph Traversal: DFS & BFS

From first principles to a complete comparison.

- [What Are DFS and BFS? A Visual Beginner's Guide to Graph Traversal (C++)](https://shafinchowdhury.dev/posts/what-is-dfs-and-bfs/): What is DFS and BFS? A visual beginner's guide to graphs, stacks and queues, with DFS and BFS traced step by step on one graph and simple C++ code. ([Markdown](https://shafinchowdhury.dev/posts/what-is-dfs-and-bfs.md))
- [DFS vs BFS: Complete Comparison With C++ Code, Examples & Use Cases](https://shafinchowdhury.dev/posts/dfs-vs-bfs/): DFS vs BFS on one graph: stack vs queue, traversal order, O(V + E) time, memory, shortest paths, cycle detection and connected components, with C++ code. ([Markdown](https://shafinchowdhury.dev/posts/dfs-vs-bfs.md))
