1143 Lowest Common Ancestor(30 分)【最近公共祖先】

6 篇文章 0 订阅

二叉搜索树的建树和寻找最近公共祖先(题目给出了BST的前序遍历,而前序遍历升序排列就是BST的中序遍历了)

#include <bits/stdc++.h>
using namespace std;

typedef struct node *Node;
typedef struct node{
    int val;
    Node l,r;
}node;

Node T;
int M,N;
int pre[10001];

Node buildTree(int s,int e)
{
    if(e < s) return NULL;
    if(s == e){
        Node tnode = (Node)malloc(sizeof(node));
        tnode->val = pre[s];
        tnode->l = NULL;
        tnode->r = NULL;
        return tnode;
    }
    int i = s+1;
    while(i<=e && pre[i]<pre[s])
        i++;
    Node tnode = (Node)malloc(sizeof(node));
    tnode->val = pre[s];
    tnode->l = buildTree(s+1,i-1);
    tnode->r = buildTree(i,e);
    return tnode;
}

bool findd(Node tree, int a, int b,int f)
{
    if(f == 0){
        if(tree == NULL){
            printf("ERROR: %d and %d are not found.\n", a, b);
			return true;
        }
        if(a<tree->val && b<tree->val)return findd(tree->l,a,b,0);
        else if(a>tree->val && b>tree->val) return findd(tree->r,a,b,0);
        else{
            bool fda = tree->val == a || findd(tree->l,a,0,1) ||  findd(tree->r,a,0,1);
            bool fdb = tree->val == b || findd(tree->l,b,0,1) ||  findd(tree->r,b,0,1);
            if(fda&&fdb){
                if(tree->val == a||tree->val == b)
                    printf("%d is an ancestor of %d.\n", tree->val == a ? a : b, tree->val == b ? a : b);
                else
                    printf("LCA of %d and %d is %d.\n", a, b, tree->val);
            }
            else if (fda == false && fdb == false) {
				printf("ERROR: %d and %d are not found.\n", a, b);
			}
			else {
				printf("ERROR: %d is not found.\n", fda ? b : a);
			}
			return true;
        }
    }
    else{
        if(tree == NULL) return false;
        if(tree->val == a) return true;
        if(tree->val > a)return findd(tree->l,a,0,1);
        return findd(tree->r,a,0,1);
    }

}

int main()
{
    cin>>M>>N;
    int a,b;
    for(int i=0;i<N;i++){
        cin>>pre[i];
    }
    T = buildTree(0,N-1);
    for(int i=0;i<M;i++){
        cin>>a>>b;
        findd(T, a, b, 0);
    }

    return 0;
}

 

看到有一种非常巧妙的方法,都不需要建树:

https://www.liuchuo.net/archives/4616

map<int, bool> mp用来标记树中所有出现过的结点,遍历一遍pre数组,将当前结点标记为a,如果u和v分别在a的左、右,或者u、v其中一个就是当前a,即(a >= u && a <= v) || (a >= v && a <= u),说明找到了这个共同最低祖先a,退出当前循环,最后根据要求输出结果即可

#include <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, bool> mp;
int main() {
    int m, n, u, v, a;
    scanf("%d %d", &m, &n);
    vector<int> pre(n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &pre[i]);
        mp[pre[i]] = true;
    }
    for (int i = 0; i < m; i++) {
        scanf("%d %d", &u, &v);
        for(int j = 0; j < n; j++) {
            a = pre[j];
            if ((a >= u && a <= v) || (a >= v && a <= u)) break;
        } 
        if (mp[u] == false && mp[v] == false)
            printf("ERROR: %d and %d are not found.\n", u, v);
        else if (mp[u] == false || mp[v] == false)
            printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
        else if (a == u || a == v)
            printf("%d is an ancestor of %d.\n", a, a == u ? v : u);
        else
            printf("LCA of %d and %d is %d.\n", u, v, a);
    }
    return 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、付费专栏及课程。

余额充值