zoukankan      html  css  js  c++  java
  • Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree

    1、Lowest Common Ancestor of a Binary Search Tree

    Total Accepted: 42225 Total Submissions: 111243 Difficulty: 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 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.

     
    /**
     * 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) {
            if(root->val >= min(p->val,q->val) && root->val <= max(p->val,q->val)) return root;
            if(root->val >=max(p->val,q->val)) return lowestCommonAncestor(root->left,p,q) ;
            else return lowestCommonAncestor(root->right,p,q);
        }
    };

    2、Lowest Common Ancestor of a Binary Tree

    Total Accepted: 26458 Total Submissions: 95470 Difficulty: Medium

    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.

    /**
     * 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) {
            
            if(root == p || root == q || root == NULL) {
                return root;
            }
            
            TreeNode* left  = lowestCommonAncestor(root->left ,p,q);
            TreeNode* right = lowestCommonAncestor(root->right,p,q);
            
            if(left==NULL && right==NULL){
                return NULL;
            }
            
            if(left!=NULL && right==NULL){
                return left;
            }
            
            if(right!=NULL && left == NULL){
                return right;
            }
            
            return root;
        }
    };

    方法2.

    /**
     * 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 {
    private:
        bool getPath(TreeNode* root,TreeNode* node,list<TreeNode*>& list_path){
            if(root==NULL){
                return false;    
            } 
            
            list_path.push_back(root);
            
            if(root == node) {
                return true;
            }
            
            bool in_left = getPath(root->left,node,list_path);
            if(in_left){
                return true;
            }
            
            bool in_right = getPath(root->right,node,list_path);
            
            if(!in_right){
                list_path.pop_back();
            }
            
            return in_right;
        }
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
            list<TreeNode*> list_path_p,list_path_q;
            
            bool list_path_p_exist = getPath(root,p,list_path_p);
            bool list_path_q_exist = getPath(root,q,list_path_q);
            
            if(!list_path_p_exist && !list_path_q_exist){
                return NULL;
            }
            
            TreeNode* res = NULL;
            while(list_path_p.front() == list_path_q.front()){
                res = list_path_p.front();
                list_path_p.pop_front();
                list_path_q.pop_front();
            }
            
            return res;
        }
    };
  • 相关阅读:
    java中计算两个时间差
    增强for循环用法
    SQLServer类型与Java类型转换问题解决
    有关SQL模糊查询
    js 弹出div窗口 可移动 可关闭 (转)
    登陆sqlserver及修改端口号 (转)
    C#判断IP地址是否合法函数-使用正则表达式-2个 (转)
    c#图像处理入门(-bitmap类和图像像素值获取方法) 转
    MongoDB C#驱动中Query几个方法 (转)
    微信公众帐号自定义菜单创建及事件响应开发教程 附源代码(转)
  • 原文地址:https://www.cnblogs.com/zengzy/p/5054246.html
Copyright © 2011-2022 走看看