PAT 1143—— Lowest Common Ancestor(二叉排序树 + 最低公共祖先)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ming991301630/article/details/79646384

中等难度题吧,求二叉排序树的最低公共祖先

注意:

1、测试点345测试数据比较大,函数在写的时候尽量避免递归操作,所有的递归操作改成while
2、我写了两个搜索函数,搜索功能一样,只是第二个搜索函数添加了LCA的判断操作(如果通过搜索两个值的所有祖先存到vector中之后再判断两个vector来找到LCA的话测试点4会一直超时)

#include <cstdio>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
struct TreeNode
{
    int value;
    TreeNode *left, *right;
    TreeNode(const int x) :value(x), left(nullptr), right(nullptr) {}
};
TreeNode *root = nullptr;
void insertBST(int value)
{
    if (root == nullptr)root = new TreeNode(value);
    TreeNode *node = root;
    TreeNode *lastNode = root;
    bool leftOrRight;//left:true;right:false
    while (node != nullptr)
    {

        if (value < node->value)
        {
            lastNode = node;
            node = node->left;
            leftOrRight = true;
        }
        else
        {
            lastNode = node;
            node = node->right;
            leftOrRight = false;
        }
    }
    if(leftOrRight)
        lastNode->left = new TreeNode(value);
    else
        lastNode->right = new TreeNode(value);
}
int findBST(TreeNode *node, const int &value, vector<int> &path)
{
    while (node != nullptr)
    {
        path.push_back(node->value);
        if (node->value == value)
            return 1;
        if(value < node->value)
            node = node->left;
        else
            node = node->right;
    }
    return -1;
}
int LCA;
int findBST111(TreeNode *node, const int &value, vector<int> &path)
{
    int pathIndex = 0;
    bool ifStop = false;
    LCA = node->value;
    while (node != nullptr)
    {
        if (!ifStop)
        {
            if(node->value != path[pathIndex])
            {
                ifStop = true;
                continue;
            }
            LCA = node->value;
            if (pathIndex < path.size() - 1)
                pathIndex++;
        }
        if (node->value == value)
            return 1;
        if (value < node->value)
            node = node->left;
        else
            node = node->right;
    }
    return -1;
}
int main()
{
    int Mtest, Nnode;
    scanf_s("%d%d", &Mtest, &Nnode);
    for(int i = 0; i < Nnode; i++)
    {
        int value;
        scanf_s("%d", &value);
        insertBST(value);
    }
    for (int i = 0; i < Mtest; ++i)
    {
        int node1, node2;
        scanf_s("%d%d", &node1, &node2);
        vector<int>Path1;
        int findRes1 = findBST(root, node1, Path1), findRes2 = findBST111(root, node2, Path1);
        if(findRes1 == -1 && findRes2 == -1)
        {
            cout << "ERROR: " << node1 << " and " << node2 << " are not found." << endl;
            continue;
        }else if(findRes1 == -1 && findRes2 == 1)
        {
            cout << "ERROR: " << node1 << " is not found." << endl;
            continue;
        }else if(findRes1 == 1 && findRes2 == -1)
        {
            cout << "ERROR: " << node2 << " is not found." << endl;
            continue;
        }

        if (LCA == node1)
        {
            cout << node1 << " is an ancestor of " << node2 << "." << endl;
        }else if(LCA == node2)
        {
            cout << node2 << " is an ancestor of " << node1 << "." << endl;
        }else
        {
            cout << "LCA of " << node1 << " and " << node2 << " is " << LCA << "." << endl;
        }
    }
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Ming991301630/article/details/79646384