Lowest Common Ancestor of a Binary Tree

原创 2015年11月20日 15:49:40
  1. 问题

    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.

  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 Solution1 {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
        {
            if(root==NULL) return NULL;
            if(p==root || q==root) return root;
            
            TreeNode *pleft=lowestCommonAncestor(root->left,p,q);
            TreeNode *pright=lowestCommonAncestor(root->right,p,q);
            
            if(pleft!=NULL && pright!=NULL) return root;
            else if(pleft != NULL)  
                return pleft;  
            else if (pright != NULL)  
                return pright;  
            else   
                return NULL; 
         }
    };
    
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) 
        {
            if(root==NULL) return NULL;
            if(p==root || q==root) return root;
            
            vector<TreeNode*> path1;
            vector<TreeNode*> path2;
             TreeNode *res1=NULL;
           if( GetNodePath(root,p,path1) && GetNodePath(root,q,path2))
            {
               int m=min(path1.size(),path2.size());
            for(int i=0;i<m;i++)
            {
                if(path1[i]==path2[i])
                {
                    res1=path1[i];
                }
                else
                {
                    break;
                }
            }
            }
            return res1;
         }
    
    
       
    public:
      bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::vector<TreeNode*>& path)
      {
         path.push_back(pHead);
        if(pHead == pNode)
          return true;
          
        bool found = false;
        if(pHead->left != NULL)
            found = GetNodePath(pHead->left, pNode, path);
        if(!found && pHead->right)
            found = GetNodePath(pHead->right, pNode, path);
        if(!found)
            path.pop_back();
    
        return found;
    
       }
    };


版权声明:本文为博主原创文章,未经博主允许不得转载。

sortList and Lowest Common Ancestor of a Binary Search Tree

首先解析二叉树 Lowest Common Ancestor of a Binary Search Tree ,寻找二叉树中某两个节点中路径最短的根节点。如下,2与8 ,最短路径根节点为6. __...

LeetCode Lowest Common Ancestor of a Binary Tree

题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. ...

leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归

原题链接:236. Lowest Common Ancestor of a Binary Tree 【思路-Java】dfs|递归实现 本题是二叉树的深度遍历的典型应用,基础还是二叉树的遍历。 ...

LeetCode 236 Lowest Common Ancestor of a Binary Tree (LCA)

LeetCode 236 Lowest Common Ancestor of a Binary Tree (LCA)

*Lowest Common Ancestor of a Binary Tree解析

Lowest Common Ancestor of a Binary Tree Total Accepted: 6162 Total Submissions: 23311 My Submissions...

[LeetCode]235. Lowest Common Ancestor of a Binary Search Tree

Problem DescriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two gi...

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

Lowest Common Ancestor of a Binary Tree

题目236:Lowest Common Ancestor of a Binary Tree题目描述: 一个普通二叉树的两个结点n1和n2的最低公共祖先。 思路: 接上题,区别在上题是二叉搜索树,...

[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 t...

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 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Lowest Common Ancestor of a Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)