Binary Search Tree : Lowest Common Ancestor

  • + 0 comments

    C#:

    public Node LowestCommonAncestor(Node n1, Node n2)
    {
        if (n1.FindNode(n2.Data) != null) return n1;
        if (n2.FindNode(n1.Data) != null) return n2;
        if(n1.Parent ==  n2.Parent) return n1.Parent;
        return LowestCommonAncestor(n1.Parent, n2.Parent);
    }