235. (LCA) Lowest Common Ancestor of a Binary Search Tree

ps. ้€™้กŒ็”จ leetcode 236 ไนŸๅฏไปฅ่งฃๆฑบ, ้€Ÿๅบฆ้‚„ๅทฎไธๅคš...ไฝ†ๅ› ็‚บ้€™้กŒๆ˜ฏ BST, ๆ‰€ไปฅไฝœๆณ•ๆ›ด็ฐกๅ–ฎ

/*

235. Lowest Common Ancestor of a Binary Search Tree

level: easy

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/

BST LCA

ๅฆ‚ๆžœๅช็œ‹็ฅ–ๅ…ˆ, ๆ‰€ไปฅ 3, 5 ็ฅ–ๅ…ˆๆœ‰ 6, 2, 4

ไฝ†ๆœ€็Ÿฎ็š„ lca, ไธ€ๅฎšๆœƒ่ฎ“ 3, 5 ๅœจๅทฆๅณๅ…ฉๅด

ๅ…ถไป–ๅฏไปฅ็™ผ็พ,  3,5 ้ƒฝๅœจ ๆŸไธ€ๅด๏ผˆ6, 2 ็š„ๆŸไธ€ๅด

ๅ› ็‚บ bst ๆœ‰ๅทฆๅฐๅณๅคง็š„็‰นๆ€ง ๏ผˆๅทฆๅญๆจนๆ‰€ๆœ‰็ฏ€้ปž็š†ๅฐๆ–ผroot, ๅณๅญๆจนๆ‰€ๆœ‰็ฏ€้ปž็š†ๅคงๆ–ผroot

1. ๆ‰€ไปฅpqๅŒๆ™‚ๆฏ”root ๅคงๆ™‚, ไปฃ่กจpqๅœจๅณ้‚Š, ๆ‰€ไปฅ่ฆๅพ€ๅณ่ตฐ
2. ๆ‰€ไปฅpqๅŒๆ™‚ๆฏ”root ๅฐๆ™‚, ไปฃ่กจpqๅœจๅทฆ้‚Š, ๆ‰€ไปฅ่ฆๅพ€ๅทฆ่ตฐ
3. ๅ…ถไป–็‹€ๆณ, ไปฃ่กจๆ‰พๅˆฐไบ†, p qๅœจๅทฆๅณๅ…ฉๅดไบ†

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.

    LCS:

    ๅˆคๆ–ทๆ–นๅผ: ๆ‰พๅˆฐ LCS็š„ๆ–นๅผ p, q ไธๅœจๆญค LCS ๅŒไธ€ๅด

       //่ชชๆ˜Ž1
       if (pVal > parentVal && qVal > parentVal) {

            // ๅพ€ๅณ่ตฐ๏ฝž๏ฝž
            return lowestCommonAncestor(root.right, p, q);

        } else if (pVal < parentVal && qVal < parentVal) { //่ชชๆ˜Ž2
            // ๅพ€ๅทฆ่ตฐ๏ฝž๏ฝž
            return lowestCommonAncestor(root.left, p, q);

        } else {//่ชชๆ˜Ž3
            //ๆ‰พๅˆฐไบ† We have found the split point, i.e. the LCA node.
            return root;
        }

Time Complexity: O(N),
where N is the number of nodes in the BST. In the worst case we might
be visiting all the nodes of the BST.

Space Complexity: O(N). This is because the maximum amount of
space utilized by the recursion stack would be N since the height of a
skewed BST could be N
 */
 
public class LowestCommonAncestorBST {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int parentVal = root.val;
        int pVal = p.val;
        int qVal = q.val;

        if (pVal > parentVal && qVal > parentVal) {
            return lowestCommonAncestor(root.right, p, q);

        } else if (pVal < parentVal && qVal < parentVal) {
            return lowestCommonAncestor(root.left, p, q);

        } else { //terminator
            return root;
        }
    }
}

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        int value1 = Math.min(p.val, q.val);
        int value2 = Math.max(p.val, q.val);
        return dfs(root, value1, value2);
    }
    private TreeNode dfs(TreeNode node, int value1, int value2) {
        if (node == null) {
            return null;
        }
        if (node.val > value2) {
            return dfs(node.left, value1, value2);
        }
        if (node.val < value1) {
            return dfs(node.right, value1, value2);
        }
        // value1 <= node.val <= value2
        return node;
    }
}
```

Last updated