LeetCode/Lowest Common Ancester Of A Binary Search Tree Or A Binary Tree

Problem Summary

  1. Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

  2. What if the tree is just a binary tree ?

Solution

Subproblem 1 is trivial if we know the definition of BST.

Subproblem 2 needs more thinking. There are two ways to solve it.

  1. The first is traditional. We define a variant LCA and a function dfs(TreeNode *ROOT,TreeNode *p,TreeNode *q). dfs returns the number of given nodes that are in the subtree with ROOT as root. If dfs returns 2 and LCA have not been changed, then the answer is ROOT.

  2. Let us define a recursive function f(TreeNode *ROOT,TreeNode *p,TreeNode *q).

    • If the subtree with ROOT as root contains both p and q, f returns the LCA of p and q.
    • If the subtree only contains one of them, f returns that one.
    • If the subtree contains neither of them, f returns NULL.

For each ROOT, we first check whether it is NULL or equal to p or q. If that is the case, return ROOT. Then we call f(ROOT->left,p,q) and f(ROOT->right,p,q), and store the results in LEFT and RIGHT. If they are both not NULL, we know that both subtrees contain one of p and q. Thus ROOT is the LCA we are looking for. If only one of them are not NULL, then return it. If both are NULL, return NULL.

Code 1 — LCA of a binary search tree

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || p == NULL || q == NULL)
return NULL;
if (root->val > p->val && root->val > q->val)
return lowestCommonAncestor(root->left,p,q);
if (root->val < p->val && root->val < q->val)
return lowestCommonAncestor(root->right,p,q);
return root;
}
};

Code 2 — LCA of a binary tree, Solution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode(int x) : val(x), left(NULL), right(NULL) {}
// };
class Solution {
public:
TreeNode *LCA;
int dfs(TreeNode *root,TreeNode *p,TreeNode *q)
{
if (root == NULL)
return 0;
int count = 0;
if (p == root)
++ count;
if (q == root)
++ count;
count += dfs(root->left,p,q);
if (count == 2)
{
if (!LCA)
LCA = root;
return 2;
}
count += dfs(root->right,p,q);
if (count == 2 && !LCA)
LCA = root;
return count;
}
TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode *p,TreeNode *q) {
LCA = NULL;
dfs(root,p,q);
return LCA;
}
};

Code 3 – LCA of a binary tree, Solution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode(int x) : val(x), left(NULL), right(NULL) {}
// };
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root,TreeNode *p,TreeNode *q) {
if (root == NULL || root == p || root == q)
return root;
TreeNode *l = lowestCommonAncestor(root->left,p,q);
TreeNode *r = lowestCommonAncestor(root->right,p,q);
if (l && r)
return root;
return l ? l:r;
// return l ? root->left:root->right;
}
};