MentorNode
Start free

Pattern 05 of 08 · tree-bfs-dfs

Tree BFS & DFS

Explore hierarchical and recursive branching structures either level-by-level (queue/BFS) or depth-first (call stack/DFS) to aggregate path invariants.

Builds on
Nothing — a good place to start.
Unlocks
Backtracking & Decision Trees

See the invariant

Step through it
tree-bfs-dfsInteractive Sandbox

Curated visualizer for tree-bfs-dfs is available via Socratic AI Mentor steps and the interactive playground below.

Pattern InvariantTwo Pointers

Converging search on sorted array: Target Sum = 26

Step 1 of 3
L
2
[0]
7
[1]
11
[2]
15
[3]
19
[4]
R
23
[5]
nums[0] (2)+nums[5] (23)=25<Target (26)

Sum 25 < target (26). Since array is sorted, we MUST increase left pointer (L: 0 to 1) to seek a larger sum.

Speed:

Practice it

3 problems
  1. 1Maximum Depth of Binary Treemaximum-depth-of-binary-treeeasy
  2. 2Binary Tree Level Order Traversalbinary-tree-level-order-traversalmedium
  3. 3Lowest Common Ancestor of a Binary Treelowest-common-ancestor-of-a-binary-treemedium