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;
    }
};
posted @ 2015-12-17 15:11  zengzy  阅读(182)  评论(0编辑  收藏  举报
levels of contents