---
# System prepended metadata

title: LC 235. Lowest Common Ancestor of a Binary Search Tree
tags: [medium, leedcode, c++]

---

# LC 235. Lowest Common Ancestor of a Binary Search Tree

### [Problem link](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)

###### tags: `leedcode` `medium` `c++`

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

According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow  **a node to be a descendant of itself** ).&rdquo;

**Example 1:** 
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png" style="width: 200px; height: 190px;" />
```
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
```

**Example 2:** 
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarysearchtree_improved.png" style="width: 200px; height: 190px;" />
```
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
```

**Example 3:** 

```
Input: root = [2,1], p = 2, q = 1
Output: 2
```

 **Constraints:** 

- The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
- <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
- All <code>Node.val</code> are  **unique** .
- <code>p != q</code>
- <code>p</code> and <code>q</code> will exist in the BST.

## Solution 1
#### C++
```cpp=
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        while (root != nullptr) {
            if (root->val > p->val && root->val > q->val) {
                root = root->left;
            } else if (root->val < p->val && root->val < q->val) {
                root = root->right;
            } else {
                return root;
            }
        }
        return nullptr;
    }
};
```
## Solution 2
#### C++
```cpp=
/**
 * Definition for a binary tree node.
 * 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) {
        int val = root->val;
        if (p->val < val && q->val < val) {
            return lowestCommonAncestor(root->left, p, q);
        }
        if (p->val > val && q->val > val) {
            return lowestCommonAncestor(root->right, p, q);
        }
        return root;
    }
};
```

>### Complexity
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(n)            | O(n)             |
>| Solution 2  | O(n)            | O(n)             |

## Note
sol1, 2:
[Ref (0x3F)](https://www.bilibili.com/video/BV1W44y1Z7AR/?spm_id_from=333.788&vd_source=088937f16fb413336c0cb260ed86a1c3)
![image](https://hackmd.io/_uploads/H1W8t-48A.png)

