My Solution to Lowest Common Ancestor of a Binary Tree Part I(Top-Down Approach)

题目在:

http://www.leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html

 

自己将上述网址中的 Top-Down Approach 重写了一遍,为了练手。

这个方法是基础的方法,复杂度为O(n2),简而言之就是先判断p q是在不同的子树还是相同的子树里,如果不同,那么root就是LCA,如果相同,那么递归。

代码如下:

#include <iostream>
using namespace std;

struct Node{
    int value;
    Node *pLeft;
    Node *pRight;
};

Node *CreateNode(int v)
{
    Node *pNode = new Node();
    if (!pNode)
        return NULL;

    pNode->value = v;
    pNode->pLeft = NULL;
    pNode->pRight = NULL;

    return pNode;
}

void LinkNode(Node *root, Node *pLeftChildNode, Node *pRightChildNode)
{
    root->pLeft = pLeftChildNode;
    root->pRight = pRightChildNode;
}

bool Contain(Node *root, Node *pNode)        //这种简单的递归是面试的最基础的题,一定要牢牢掌握,一般O(n2)的算法都是基于此类pattern的
{
    if (!root || !pNode)
        return false;

    if (root->value == pNode->value)
        return true;
    else
        return (Contain(root->pLeft, pNode) || Contain(root->pRight, pNode));
}

Node *LCA(Node *root, Node *p, Node *q)
{
    if (!root || !p || !q)
        return NULL;            //通常这个return NULL;都是包括2个含义:1非法输入 2叶子节点或者递归的终止条件

    if (!Contain(root, p) || !Contain(root, q))
        return NULL;

    //如果root等于p或者q,那么root就是LCA
    if (root == p || root == q)
        return root;

    //如果p q不属于root的同一颗子树
    if ( (Contain(root->pLeft, p) && Contain(root->pRight, q)) || 
        (Contain(root->pLeft, q) && Contain(root->pRight, p)) )    //注意这里是root->pLeft和root->pRight
        return root;

    //如果p q都在左子树
    else if (Contain(root->pLeft, p) && Contain(root->pLeft, q))
        return LCA(root->pLeft, p, q);

    //如果p q都在右子树
    else if (Contain(root->pRight, p) && Contain(root->pRight, q))
        return LCA(root->pRight, p, q);
}

int main()
{
    Node *p[8];
    p[0] = CreateNode(3);
    p[1] = CreateNode(5);
    p[2] = CreateNode(1);
    p[3] = CreateNode(6);
    p[4] = CreateNode(2);
    p[5] = CreateNode(0);
    p[6] = CreateNode(8);
    p[7] = CreateNode(7);
    p[8] = CreateNode(4);

    if (!p[0] || !p[1] || !p[2] || !p[3] || !p[4] || !p[5] || !p[6] || !p[7] || !p[8])
    {
        cout<<"Create Node Err!"<<endl;
        return -1;
    }

    LinkNode(p[0], p[1], p[2]);
    LinkNode(p[1], p[3], p[4]);
    LinkNode(p[2], p[5], p[6]);
    LinkNode(p[4], p[7], p[8]);
    
    Node *pLCA = LCA(p[0], p[0], p[2]);
    Node *pLCA2 = LCA(p[0], NULL, NULL);

    if (pLCA)
        cout<<"pLCA->value = "<<pLCA->value<<endl;
    else
        cout<<"Error"<<endl;

    if (pLCA2)
        cout<<"pLCA2->value = "<<pLCA2->value<<endl;
    else
        cout<<"Error"<<endl;

    return 0;
}

 

 

 

 

end

转载于:https://www.cnblogs.com/lihaozy/archive/2012/12/03/2799783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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、付费专栏及课程。

余额充值