235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先

235. Lowest Common Ancestor of a Binary Search Tree

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

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

        _______6______
       /                  ___2__          ___8__
   /      \        /         0      _4       7       9
         /           3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    while(root) {
        if (p->val > root->val && q->val > root->val) {
            root = root->right;
            continue;
        }
        if (p->val < root->val && q->val < root->val) {
            root = root->left;
            continue;
        }
        return root;
    }
    return NULL;
}

236. Lowest Common Ancestor of a Binary Tree

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 v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
       /                  ___5__          ___1__
   /      \        /         6      _2       0       8
         /           7   4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

(1)一次递归找两个节点

TreeNode* lowestCommonAncestor02(TreeNode* root, TreeNode* p, TreeNode* q) {

    //return if found or not found, return NULL if not found
    if (root==NULL || root == p || root == q) return root;

    //find the LCA in left tree
    TreeNode* left = lowestCommonAncestor02(root->left, p, q);
    //find the LCA in right tree
    TreeNode* right = lowestCommonAncestor02(root->right, p, q);

    //left==NULL means both `p` and `q` are not found in left tree.
    if (left==NULL) return right;
    //right==NULL means both `p` and `q` are not found in right tree.
    if (right==NULL) return left;
    // left!=NULL && right !=NULL, which means `p` & `q` are seperated in left and right tree.
    return root;
}

(2)分别记录根节点到所求节点的先序遍历路径,返回路径中最后一个相同的节点

bool findPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& path) {
    if (root==NULL) return false;
    if (root == p) {
        path.push_back(p);
        return true;
    }

    path.push_back(root);
    if (findPath(root->left, p, path)) return true;
    if (findPath(root->right, p, path)) return true;
    path.pop_back();

    return false;
}

//Ordinary way, find the path and comapre the path.
TreeNode* lowestCommonAncestor01(TreeNode* root, TreeNode* p, TreeNode* q) {

    vector<TreeNode*> path1, path2;

    if (!findPath(root, p, path1)) return NULL;
    if (!findPath(root, q, path2)) return NULL;

    int len = path1.size() < path2.size() ? path1.size() : path2.size();
    TreeNode* result = root;
    for(int i=0; i<len; i++) {
        if (path1[i] != path2[i]) {
            return result;
        }
        result = path1[i];
    }
    return result;
}
时间: 2024-10-29 09:47:44

235.236. Lowest Common Ancestor of a Binary (Search) Tree -- 最近公共祖先的相关文章

LeetCode (236):Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

【LeetCode 235】Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree 递归寻找p或q,如果找到,层层向上返回,知道 root 左边和右边都不为NULL:if (left!=NULL && right!=NULL) return root; 时间复杂度 O(n),空间复杂度 O(H) class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode*

LeetCode 235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree) 32

235. 二叉搜索树的最近公共祖先 235. Lowest Common Ancestor of a Binary Search Tree 题目描述 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)." 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,nul

【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)

Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two

Lowest Common Ancestor of a Binary Search Tree &amp; a Binary Tree

235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description 题目大意:给定一棵二叉查找树和两个节点p和q,要求返回这两个节点的第一个公共祖先. 思路:对于当前节点node,如果p和q的val都大于node的val,则说明p和q的第一个公共祖先在node的右子树,如

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two

LeetCode_235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree Easy Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined be

leetcode - Lowest Common Ancestor of a Binary Search Tree

leetcode -  Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined