• 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 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.

    分析:

    在二叉查找树种,寻找两个节点的最低公共祖先。

    1、如果a、b都比根节点小,则在左子树中递归查找公共节点。此时对root->left递归求解

    2、如果a、b都比根节点大,则在右子树中查找公共祖先节点。对root->right递归求解

    3、如果a、b一个比根节点大,一个比根节点小,或者有一个等于根节点,则根节点即为最低公共祖先。

    c递归代码:

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     struct TreeNode *left;
     6  *     struct TreeNode *right;
     7  * };
     8  */
     9 struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    10     if(root == NULL || p == NULL || q == NULL)
    11             return NULL;
    12     if(root->val > p->val && root->val > q->val){
    13         return lowestCommonAncestor(root->left, p, q);
    14     }
    15     else if(root->val < p->val && root->val < q->val){
    16         return lowestCommonAncestor(root->right, p, q);
    17     }
    18     else{
    19          return root;
    20     }
    21 }

    c非递归代码:

     1 struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
     2     if(root == NULL || p == NULL || q == NULL)
     3             return NULL;
     4     while(root != NULL){
     5         if(root->val > p->val && root->val > q->val)
     6             root = root->left;
     7         else if(root->val < p->val && root->val < q->val)
     8             root = root->right;
     9         else
    10             break;
    11     }
    12     return root;
    13 }

    C++代码:

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    13         if(root == NULL || p == NULL || q == NULL)
    14             return NULL;
    15         int min1 = min(p->val, q->val);
    16         int max1 = max(p->val, q->val);
    17         //if((root->val >= p->val && root->val <= q->val) || (root->val >= q->val && root->val <= p->val))
    18          //   return root;
    19         if(max1 < root->val){
    20             return lowestCommonAncestor(root->left, p, q);
    21         }
    22         else if(min1 > root->val){
    23             return lowestCommonAncestor(root->right, p, q);
    24         }
    25         else{
    26             return root;
    27         }
    28     }
    29 };

    leetcode编译器运行,第一个程序用时最少,依次递增。

    除了这种解法,也可以把它当做普通的二叉树做。

  • 相关阅读:
    Oracle11g聚合函数
    和为S的连续正数数列,动态规划,C++
    统计一个数组在排序数组中出现的次数,C++,二分查找
    寻找两个链表的第一个公共子节点,C++
    二维数组中的查找
    数组中的逆序对,C++,分治算法
    得到从小到大的第N个丑数的三种方式(C++)一维动态规划
    连续字数组的最大和(Java)一个int数组,求其中的最大的连续数的和
    n个整数,求这中间最小的k个整数(Java)
    两个字符串的最长公共子串求法(C++、动态规划)
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/5721681.html
Copyright © 2020-2023  润新知