[LeetCode]0235/0236.Lowest Common Ancestor of a Binary Search Tree / Binary Tree

61 阅读2分钟

235.Lowest Common Ancestor of a Binary Search Tree
Description:
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

image.png

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.

Solution:
    A binary search tree follows some order to arrange the elements. In a Binary search Tree, the value of left node must be smaller than the parent node, and the value of right node must be greater than the parent node.This rule is applied recursively to the left and right subtrees of the root.

Accroding the definition of LCA, either p or q can share a LCA or one of them can be the LCA.

  1. Both p or q are smaller than the recursive item, searching the left subtree.
  2. Both p or q are greater than the recursive item, searching the right subtree.
  3. Recursive item is smaller than one of these, and grester than the other. so the LCA is Recursive item.

If we use subtraction operations to compare number sizes, particular attention need to be paid on number overflows. Test cases contain some Long data(we need change int to long, such as p.val.toLong).

fun lowestCommonAncestorOfBST(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
    if (p!!.`val` < root!!.`val` && q!!.`val` < root.`val`) {
        return lowestCommonAncestor(root.left, p, q)
    } else if (p.`val` > root.`val` && q!!.`val` > root.`val`){
        return lowestCommonAncestor(root.right, p, q)
    }
    return root
}

236.Lowest Common Ancestor of a Binary Tree
description:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

image.png

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Solution:
The p and q nodes in different subtrees or current item is p or q, current item is the LCA.
The p and q nodes in the same subtree, searching the left and right subtree of current item.

fun lowestCommonAncestorOfBT(root: TreeNode?, p: TreeNode?, q: TreeNode?): TreeNode? {
    // found a node, return
    if (root == null || root.`val` == p?.`val` || root.`val` == q?.`val`) {
        return root
    }

    val l = lowestCommonAncestor(root.left, p, q)
    val r = lowestCommonAncestor(root.right, p, q)
    // In the different subtress of the root, so root is the LCA
    if (l != null && r != null) {
        return root
    }
    // The Node find in the subtree.
    return l ?: r
}