DSA sheet · Trees
Binary Tree DSA Notebook
My study notes from the pattern-wise DSA playlist on binary trees. Each page is written so that a beginner (or me, after forgetting everything) can read it once and understand the intuition, how every condition is derived, the Python code line by line, and a full dry run.
Every page follows the same order:
① question in simple words → ② constraints → ③ intuition → ④ building the logic from examples → ⑤ approach steps → ⑥ code → ⑦ line by line → ⑧ dry run → ⑨ complexity & remember
Comparing two trees
- 1Same Tree & Symmetric TreeDFS · BFS
- 14Subtree of Another TreeDFS · BFS
- 27Merge Two Binary TreesLC 617 · merge BSTs
Level by level (BFS shape)
- 2Zigzag Level Order Traversalbrute · BFS · DFS
- 3Average of Levelsbrute · BFS · DFS
- 6Left View & Right ViewBFS (+ DFS extra)
- 7Maximum & Minimum DepthBFS · DFS
- 8Cousins in Binary TreeBFS · DFS
- 9Populating Next Right PointersBFS · DFS
- 13Maximum Width of Binary TreeBFS · DFS
Height-based recursion
- 4Diameter of Binary Treebrute BFS → DFS
- 5Balanced Binary Treebrute BFS → DFS
- 20Binary Tree Maximum Path SumDFS
- 19Count Complete Tree Nodesbrute → O(log² n)
Views & vertical lines
- 10Top View of Binary TreeBFS · DFS
- 11Bottom View of Binary TreeBFS · DFS
- 12Vertical Order TraversalBFS · DFS
- 17Boundary Traversal3-part walk
Paths
Ancestors & distance
- 18Nodes at Distance Kparent map · BFS
- 21Lowest Common AncestorDFS · parent map
- 22LCA of Deepest LeavesBFS idea → DFS
Changing the tree
- 23Invert Binary TreeDFS · BFS
- 24Flatten Binary Tree to Linked Listbrute → O(1) space
- 30Binary Tree to Doubly Linked Listbrute → optimal
Building a tree
- 25Build Tree from Preorder & Inorderbrute → hashmap
- 26Build Tree from Inorder & PostorderDFS + hashmap
- 28Build Tree from Preorder & PostorderDFS + indexes
- 29Serialize & Deserialize Binary Treepreorder DFS
Numbers are the order in the playlist. "Next →" at the top of each page follows that order.