Trees

54 articles

dsa3 min read

Delete Node in a BST

Find and delete a node from a BST while maintaining BST properties using in-order successor.

Read →
dsa2 min read

Sum Root to Leaf Numbers

Compute the total sum of all root-to-leaf numbers formed by concatenating digits along each path.

Read →
dsa3 min read

Maximum Width of Binary Tree

Find the maximum width of a binary tree where width is measured from leftmost to rightmost non-null node per level.

Read →
dsa2 min read

Convert BST to Greater Tree

Replace each node value with the sum of all values greater than or equal to it using reverse in-order traversal.

Read →
dsa2 min read

Find Duplicate Subtrees

Find all duplicate subtrees by serializing each subtree and using a hash map to detect duplicates.

Read →
dsa2 min read

Unique Binary Search Trees II

Generate all structurally unique BSTs with values 1 to n using divide and conquer with memoization.

Read →
dsa2 min read

Trim a Binary Search Tree

Trim a BST so all values lie within [low, high] by recursively pruning out-of-range subtrees.

Read →
dsa2 min read

Recover Binary Search Tree

Find and swap the two misplaced nodes in a BST using in-order traversal to detect the violations.

Read →
dsa2 min read

House Robber III

Maximize robbery from a tree-structured neighborhood where adjacent (parent-child) nodes cannot both be robbed.

Read →
dsa2 min read

Binary Tree Cameras

Find minimum cameras to monitor all nodes using greedy bottom-up: prefer placing cameras at parents of uncovered leaves.

Read →
dsa2 min read

Distribute Coins in Binary Tree

Count minimum moves to distribute coins evenly by tracking excess/deficit coins flowing through each edge.

Read →
dsa3 min read

Kth Ancestor of a Tree Node

Find the Kth ancestor of any tree node in O(log k) per query using binary lifting (sparse table DP).

Read →
dsa2 min read

Maximum Sum BST in Binary Tree

Find the maximum sum of any BST subtree within a binary tree using post-order DFS returning subtree metadata.

Read →
dsa2 min read

Find Leaves of Binary Tree

Collect leaves by height (distance from leaf) repeatedly, returning all leaves at each height level.

Read →
dsa2 min read

Count Complete Tree Nodes

Count nodes in a complete binary tree in O(log^2 n) by comparing left and right heights to identify full subtrees.

Read →
dsa3 min read

Sum of Distances in Tree

Compute sum of distances from every node to all others in O(n) using two DFS passes with rerooting technique.

Read →
dsa2 min read

Unique Binary Search Trees

Count the number of structurally unique BSTs with n nodes using Catalan numbers and DP.

Read →
dsa1 min read

Binary Tree Pruning

Remove all subtrees that do not contain a 1 by recursively returning null for subtrees with only zeros.

Read →
dsa2 min read

Binary Search Tree Iterator

Implement an in-order BST iterator with O(h) space using a stack to simulate recursive in-order traversal.

Read →
dsa2 min read

Path Sum IV

Sum all root-to-leaf path sums in a tree encoded as 3-digit integers using a hashmap to decode tree structure.

Read →
dsa2 min read

Binary Tree Upside Down

Transform a binary tree so every right child becomes a sibling and left child becomes parent using iterative rotation.

Read →
dsa2 min read

Add One Row to Tree

Insert a new row of nodes at a given depth in a binary tree using BFS to reach the target depth level.

Read →
dsa1 min read

Deepest Leaves Sum

Sum all nodes at the deepest level of a binary tree using BFS to process level by level.

Read →
dsa2 min read

Cousins in Binary Tree

Determine if two nodes are cousins (same depth, different parents) using BFS to track depth and parent.

Read →
dsa2 min read

Construct Quad Tree

Build a quad tree from a 2D grid by recursively checking if a region is uniform and splitting into four quadrants.

Read →