Trees

76 articles

dsa6 min read

Mock Week 1 — Easy and Medium Problems with Communication Focus

Week 1 of the FAANG mock interview program pairs easy and medium problems from arrays and trees. The goal is not ceiling testing — it is installing the think-aloud habits that distinguish passing candidates before difficulty ramps up in week 2.

Read →
dsa7 min read

Path Sum III — Prefix Sum HashMap on Binary Trees (LC 437)

LC 437 Path Sum III counts all paths in a binary tree summing to a target. The optimal O(n) solution mirrors the subarray sum equals k trick — use a prefix sum frequency map with DFS backtracking, a pattern tested at Amazon and Google.

Read →
dsa5 min read

Delete Node in a BST — LC 450 Interview Deep Dive

LC 450 Delete Node in a BST is a top FAANG interview problem testing all three deletion cases. Master the in-order successor strategy to pass BST questions at Amazon, Google, and Microsoft.

Read →
dsa6 min read

Populate Next Right Pointers in Each Node — LC 116 O(1) Space BFS

LC 116 Populate Next Right Pointers asks you to connect each node to its next right sibling. The O(1) space solution leverages already-connected next pointers on the current level to wire up the next level — a pattern tested at Amazon and Microsoft.

Read →
dsa6 min read

All Nodes Distance K in Binary Tree — LC 863 BFS with Parent Map

LC 863 All Nodes Distance K in Binary Tree finds all nodes exactly K edges from a target. The key insight is converting the tree to an undirected graph by recording parent pointers, then running BFS from the target — a pattern tested at Amazon and Google.

Read →
dsa6 min read

House Robber III — Tree DP with Pair Return (LC 337)

LC 337 House Robber III extends the classic House Robber DP to a binary tree where adjacent nodes cannot both be robbed. The optimal O(n) solution uses post-order DFS returning a (rob, skip) pair — a fundamental tree DP pattern tested at Amazon and Microsoft.

Read →
dsa6 min read

Binary Tree Cameras — LC 968 Greedy 3-State DFS

LC 968 Binary Tree Cameras asks for the minimum number of cameras to monitor all nodes. The O(n) greedy solution assigns three states per node in a bottom-up DFS — delay camera placement as high as possible, a pattern tested at Amazon and Google.

Read →
dsa6 min read

Distribute Coins in Binary Tree — LC 979 Post-Order Flow Analysis

LC 979 Distribute Coins in Binary Tree asks for minimum moves to give each node exactly one coin. The O(n) solution tracks coin excess flowing through each edge via post-order DFS — a pattern tested at Amazon and Google that elegantly converts a counting problem into a flow problem.

Read →
dsa6 min read

Kth Ancestor of a Tree Node — LC 1483 Binary Lifting

LC 1483 Kth Ancestor of a Tree Node answers each query in O(log k) after O(n log n) preprocessing using binary lifting — a sparse table DP technique that also powers LCA algorithms and is commonly tested at Amazon.

Read →
dsa6 min read

Binary Tree Maximum Path Sum — LC 124 Hard DFS Interview Classic

LC 124 Binary Tree Maximum Path Sum finds the highest-value path in a binary tree where the path can start and end at any node. The O(n) solution uses a post-order DFS that tracks the global max while returning only one branch to the parent — a critical FAANG interview problem at Amazon, Google, and Facebook.

Read →
dsa6 min read

Serialize and Deserialize Binary Tree — LC 297 FAANG Hard

LC 297 Serialize and Deserialize Binary Tree is a top FAANG hard problem asked at Facebook, Amazon, and Google. The DFS preorder approach with null markers gives a compact O(n) codec; BFS level-order is more intuitive. Both are valid interview answers.

Read →
dsa6 min read

Maximum Sum BST in Binary Tree — LC 1373 Post-Order Metadata

LC 1373 Maximum Sum BST in Binary Tree finds the highest sum among all BST subtrees of a binary tree. The O(n) solution uses post-order DFS returning a 4-tuple of (is_bst, min, max, sum) metadata — a hard FAANG problem tested at Amazon and Google.

Read →
dsa5 min read

Find Leaves of Binary Tree — LC 366 Height-Based Grouping

LC 366 Find Leaves of Binary Tree groups nodes by their height (distance from the nearest leaf) using a post-order DFS — a problem asked at Amazon and LinkedIn that reveals an elegant alternative to iterative leaf removal.

Read →
dsa5 min read

Path Sum IV — LeetCode 666 Encoded Tree DFS

LeetCode 666 Path Sum IV reconstructs a tree from depth-position-value encoded integers and returns the sum of all root-to-leaf paths. Solve with hashmap DFS in O(n) — frequent at Amazon and Alibaba.

Read →
dsa5 min read

Binary Tree Upside Down — Re-root the Left Spine in O(n)

LeetCode 156 (Medium) classically asked at Google and LinkedIn. Re-root a binary tree by flipping each left child into the new parent and the original parent into the new right child, in O(n) time and O(1) extra space iteratively.

Read →
dsa5 min read

Add One Row to Tree — BFS Insertion at Depth in O(n)

LeetCode 623 (Medium) asked at Amazon and Microsoft. Insert a new row of value v at a given depth, pushing existing children down as left/right subtrees of new nodes, using BFS or DFS in O(n) time.

Read →
dsa6 min read

Count Nodes in a Complete Binary Tree — LC 222 O(log^2 n) Proof

LC 222 Count Complete Tree Nodes has an O(log^2 n) solution that exploits perfect subtree detection — a FAANG interview problem where the naive O(n) answer is wrong. Learn the left/right spine height comparison trick tested at Amazon and Google.

Read →
dsa6 min read

Construct Quad Tree — LC 427 Divide and Conquer Grid

LC 427 Construct Quad Tree builds a spatial partitioning tree from a 2D binary grid by recursively splitting non-uniform regions into four quadrants. This divide-and-conquer problem is tested at Amazon and Google and models real-world image compression and spatial indexing.

Read →