*Lowest Common Ancestor of a Binary Tree解析

Lowest Common Ancestor of a Binary Tree Total Accepted: 6162 Total Submissions: 23311 My Submissions Question Solution
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.
正确的做法:
检查一个节点是否同时包含需要检查的两个节点,如果包含那么继续检查他的左右孩子,如果孩子节点不同时包含这两个节点,那么根节点就是最小祖先节点
这里虽然遍历会带来很多重复的检查,但是这里可以很好的排出一种情况,就是子节点中有一些相同的节点数量

/**
 * 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) {
        TreeNode *result = NULL;  
        findLCA(root, p, q, &result);  

        return result;  
    }
private:  
    bool findNode(TreeNode *root, TreeNode *p)  
    {  
        if (root == NULL || p == NULL)  
        {  
            return false;  
        }  

        if (root == p)  
        {  
            return true;  
        }  

        return findNode(root->left, p) || findNode(root->right, p);  
    }  

    int findLCA(TreeNode *root, TreeNode *p, TreeNode *q, TreeNode **result)  
    {  
        if (root == NULL)  
        {  
            return 0;  
        }  

        if (root == p)  
        {  
            if (findNode(root, q))  
            {  
                *result = root;  
                return 2;  
            }  
            else  
            {  
                return 1;  
            }  
        }  
        else if (root == q)  
        {  
            if (findNode(root, p))  
            {  
                *result = root;  
                return 2;  
            }  
            else  
            {  
                return 1;  
            }  
        }  
        else   
        {  
            int left = findLCA(root->left, p, q, result);  
            int right = 0;  
            if (left != 2)  
            {  
                right = findLCA(root->right, p, q, result);  
            }  
            if (left == 1 && right == 1)  
            {  
                *result = root;  
            }  
            return left + right;  
        }  
    }  
};

这里首先是完成了一个常规的寻找最小父亲节点的算法,但是有一个测试样例没有办法通过,这里的思路是,从根节点开始查找到包含需要检测的节点的路径,最后变化为寻找最大公共节点的问题,但是如果出现重复的节点就不能想象哪个路径是正确的
如果数中有重复的node那么,必须寻找到最小的父亲节点

/**
 * 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 == NULL || p == NULL || q == NULL)
        {
            return NULL;
        }
        vector<TreeNode *> path1;
        path1.clear();
        GetNodePath(root,p,path1);
        vector<TreeNode *> path2;
        path2.clear();
        GetNodePath(root,q,path2);
        return FoundCommon(path1,path2);
}
int GetNodePath(TreeNode *root,TreeNode *node,vector<TreeNode *> &path)
{
    if(root == NULL)
    {
        return 0;
    }
    if(root->val == node->val)
    {
        path.push_back(root);
        return 1;
    }
    int found = 0;
    path.push_back(root);
    found = GetNodePath(root->left,node,path);
    if(found == 1)
    {
        return 1;
    }
    found = GetNodePath(root->right,node,path);
    if(found == 1)
    {
        return 1;
    }
    else
    {
        path.pop_back();
        return 0;
    }
}
TreeNode * FoundCommon(vector<TreeNode *> path1,vector<TreeNode *> path2)
{
    if(path1.empty() || path2.empty() || path1[0] != path2[0])
    {
        return NULL;
    }
    int i = 1;
    int j = 1;
    TreeNode *Root = path1[0];
    while(i < path1.size() && j < path2.size())
    {
        if(path1[i]->val == path2[j]->val)
        {
            Root = path1[i];
            i++;
            continue;
        }
        else
        {
            return Root;
        }
    }
    return Root;
}
};

根据上面的问题,在寻找路径的过程中将所有中间的值保留,结果进行比较输出,然后找到路径深度最大的那个节点:

/**
 * 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 == NULL || p == NULL || q == NULL)
        {
            return NULL;
        }
        vector<TreeNode *> path1;
        vector<vector<TreeNode *>> tempPath1;
        path1.clear();
        GetNodePath(root,p,path1,tempPath1);
        vector<TreeNode *> path2;
        vector<vector<TreeNode *>> tempPath2;
        path2.clear();
        GetNodePath(root,q,path2,tempPath2);
        return Found(tempPath1,tempPath2);
}
void GetNodePath(TreeNode *root,TreeNode *node,vector<TreeNode *> &path,vector<vector<TreeNode *>> &tempPath)
{
    if(root == NULL)
    {
        return;
    }
    if(root->val == node->val)
    {
        path.push_back(root);
        tempPath.push_back(path);
        return;
    }
    path.push_back(root);
    GetNodePath(root->left,node,path,tempPath);
    GetNodePath(root->right,node,path,tempPath);
    path.pop_back();
    return;
}
TreeNode * Found(vector<vector<TreeNode *>> &tempPath1,vector<vector<TreeNode *>> &tempPath2)
{
    if(tempPath1.empty() || tempPath2.empty())
    {
        return NULL;
    }
    int i = 1;
    int j = 1;
    int ii = 0;
    int jj = 0;
    int maxLength = 1;
    TreeNode * min = tempPath1[0][0];
    TreeNode * Root = tempPath1[0][0];
    int findFlag = 0;
    for(; i < tempPath1.size(); i++)
    {
        for(; j < tempPath2.size();j++)
        {
            ii = 0;
            findFlag = 0;
            while(ii < tempPath1[i].size() && ii < tempPath2[j].size())
            {
                if(tempPath1[i][ii]->val == tempPath2[j][ii]->val)
                {
                    Root = tempPath1[i][ii];
                    ii++;
                }
                else
                {
                    if((ii > maxLength) && (!findFlag))
                    {
                        maxLength = ii;
                        min = Root;
                        findFlag = 1;
                    }
                }
            }

        }
    }
    return min;
}
};

其实这个问题,还有很多后续的思考:
比如:如果树是二叉搜索树,那么直接利用递归操作就能比较好的找到节点
如果树有父亲节点,那么可以反相构建到根节点的路径,寻找第一个公共自节点
同理,如果是完全二叉树,那么也就很容易知道父亲节点。。。

以下是C#中二叉树lowest common ancestor的源代码: ```csharp using System; public class Node { public int value; public Node left; public Node right; public Node(int value) { this.value = value; this.left = null; this.right = null; } } public class BinaryTree { public Node root; public BinaryTree() { this.root = null; } public Node LowestCommonAncestor(Node node, int value1, int value2) { if (node == null) { return null; } if (node.value == value1 || node.value == value2) { return node; } Node left = LowestCommonAncestor(node.left, value1, value2); Node right = LowestCommonAncestor(node.right, value1, value2); if (left != null && right != null) { return node; } return (left != null) ? left : right; } } public class Program { public static void Main() { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); Node lca = tree.LowestCommonAncestor(tree.root, 4, 5); Console.WriteLine("Lowest Common Ancestor of 4 and 5: " + lca.value); lca = tree.LowestCommonAncestor(tree.root, 4, 6); Console.WriteLine("Lowest Common Ancestor of 4 and 6: " + lca.value); lca = tree.LowestCommonAncestor(tree.root, 3, 4); Console.WriteLine("Lowest Common Ancestor of 3 and 4: " + lca.value); lca = tree.LowestCommonAncestor(tree.root, 2, 4); Console.WriteLine("Lowest Common Ancestor of 2 and 4: " + lca.value); } } ``` 在上面的代码中,我们定义了一个Node类和一个BinaryTree类。我们使用BinaryTree类来创建二叉树,并实现了一个LowestCommonAncestor方法来计算二叉树中给定两个节点的最近公共祖先。 在LowestCommonAncestor方法中,我们首先检查给定节点是否为null或与给定值之一匹配。如果是,则返回该节点。否则,我们递归地在左子树和右子树上调用LowestCommonAncestor方法,并检查它们的返回值。如果左子树和右子树的返回值都不为null,则当前节点是它们的最近公共祖先。否则,我们返回非null的那个子树的返回值。 在Main方法中,我们创建了一个二叉树,并测试了LowestCommonAncestor方法的几个不同输入。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值