Skip to content
We areTOP 1%
DSA · Pro · Pattern 7 of 9

Advanced Trees (BST, LCA, Tree DP)

16 problems.
Your progress0%
1st Visit
0/16
2nd Visit
0/16
Revise
0/16

About this pattern

Tree problems at this level rely on three ideas beyond basic traversal. In a binary search tree, in-order traversal visits keys in sorted order, which makes validation, recovery and deletion systematic. For ancestor and path queries on a rooted tree, binary lifting stores the 2^j-th ancestor of every node and answers k-th ancestor and lowest common ancestor queries in O(log n). Tree DP returns a small tuple from each subtree, such as the best result with and without its root, and rerooting reuses those values to answer the question for every possible root in O(n). Signals: ancestor or path queries, or an answer required for every vertex.

const int LOG = 18, MAXN = 200005;
vector<int> adj[MAXN];
int up[LOG][MAXN], dep[MAXN];

void dfs(int u, int p) {                     // call dfs(root, root)
    up[0][u] = p;
    for (int j = 1; j < LOG; j++) up[j][u] = up[j - 1][up[j - 1][u]];
    for (int v : adj[u]) if (v != p) { dep[v] = dep[u] + 1; dfs(v, u); }
}
int lca(int a, int b) {
    if (dep[a] < dep[b]) swap(a, b);
    for (int j = LOG - 1; j >= 0; j--)
        if (dep[a] - (1 << j) >= dep[b]) a = up[j][a];
    if (a == b) return a;
    for (int j = LOG - 1; j >= 0; j--)
        if (up[j][a] != up[j][b]) { a = up[j][a]; b = up[j][b]; }
    return up[0][a];
}

Problems

Advanced Trees (BST, LCA, Tree DP) questions
#ProblemPlatformDifficultyOpen1st1st Visit2nd2nd VisitRevRevise
1Lowest Common Ancestor of a Binary Search TreeLeetCodeMedium Solve Lowest Common Ancestor of a Binary Search Tree on LeetCode (opens in a new tab)
2Delete Node in a BSTLeetCodeMedium Solve Delete Node in a BST on LeetCode (opens in a new tab)
3Construct Binary Search Tree from Preorder TraversalLeetCodeMedium Solve Construct Binary Search Tree from Preorder Traversal on LeetCode (opens in a new tab)
4Binary Search Tree IteratorLeetCodeMedium Solve Binary Search Tree Iterator on LeetCode (opens in a new tab)
5Recover Binary Search TreeLeetCodeMedium Solve Recover Binary Search Tree on LeetCode (opens in a new tab)
6House Robber IIILeetCodeMedium Solve House Robber III on LeetCode (opens in a new tab)
7Military ProblemCodeforcesMedium 1600Solve Military Problem on Codeforces (opens in a new tab)
8Infected TreeCodeforcesMedium 1600Solve Infected Tree on Codeforces (opens in a new tab)
9Parsa's Humongous TreeCodeforcesMedium 1600Solve Parsa's Humongous Tree on Codeforces (opens in a new tab)
10Choosing Capital for TreelandCodeforcesMedium 1700Solve Choosing Capital for Treeland on Codeforces (opens in a new tab)
11Distance in TreeCodeforcesHard 1800Solve Distance in Tree on Codeforces (opens in a new tab)
12Maximum White SubtreeCodeforcesHard 1800Solve Maximum White Subtree on Codeforces (opens in a new tab)
13Kth Ancestor of a Tree NodeLeetCodeHard Solve Kth Ancestor of a Tree Node on LeetCode (opens in a new tab)
14Maximum Sum BST in Binary TreeLeetCodeHard Solve Maximum Sum BST in Binary Tree on LeetCode (opens in a new tab)
15Fools and RoadsCodeforcesHard 1900Solve Fools and Roads on Codeforces (opens in a new tab)
16Sum of Distances in TreeLeetCodeHard Solve Sum of Distances in Tree on LeetCode (opens in a new tab)

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account