Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于查找最低公共祖先的代码在某些测试用例中不起作用_C++_Tree_Binary Search Tree - Fatal编程技术网

C++ 用于查找最低公共祖先的代码在某些测试用例中不起作用

C++ 用于查找最低公共祖先的代码在某些测试用例中不起作用,c++,tree,binary-search-tree,C++,Tree,Binary Search Tree,我在Hackerrank做这个练习 在本练习中,我们将获得一个指向二叉搜索树根的指针以及两个值v1和v2。我们需要返回二进制搜索树中v1和v2的最低共同祖先LCA。我们必须完成lca功能。它应该返回一个指针,指向给定的两个值中最低的公共祖先节点 lca具有以下参数: 根:指向二叉搜索树的根节点的指针 v1:一个节点。数据值 v2:节点数据值 我知道递归解决方案更容易,但我尝试了这种方法,没有找到任何失败测试用例的原因。下面提到的解决方案仅通过6/10个测试用例。我不能看到所有4个失败的测试用例,

我在Hackerrank做这个练习 在本练习中,我们将获得一个指向二叉搜索树根的指针以及两个值v1和v2。我们需要返回二进制搜索树中v1和v2的最低共同祖先LCA。我们必须完成lca功能。它应该返回一个指针,指向给定的两个值中最低的公共祖先节点

lca具有以下参数:

根:指向二叉搜索树的根节点的指针

v1:一个节点。数据值

v2:节点数据值 我知道递归解决方案更容易,但我尝试了这种方法,没有找到任何失败测试用例的原因。下面提到的解决方案仅通过6/10个测试用例。我不能看到所有4个失败的测试用例,但我可以提到1个测试用例

这是我的密码

/*The tree node has data, left child and right child 
class Node {
    int data;
    Node* left;
    Node* right;
};

*/

vector<Node*> find(int v, Node* root)

{
    vector<Node*> ar;
    if (v == root->data) {

        ar.push_back(root);
        return ar;
    }

    if (v > root->data) {
        ar.push_back(root);
        find(v, root->right);
    }

    if (v < root->data) {
        ar.push_back(root);
        find(v, root->left);
    }
    ar.push_back(root);
    return ar;
}

Node* lca(Node* root, int v1, int v2)
{

    //this vector contains the nodes in path
    //to reach from root to node
    vector<Node *> a, b;

    //find function return the vector
    a = find(v1, root);
    b = find(v2, root);

    //index of the LCA in the vector
    int res = 0;

    //traversing the two vectors from
   //beg to end if two nodes are same
  //update the value of res. Final updated
 //value will give us LCA
    int n = b.size();
    if (a.size() < b.size())
        n = a.size();
    int i = 0;
    while (i < n) {
        if (a[i] == b[i])
            res = i;

        i++;
    }
    return a[res];
}
失败的测试用例:

八,

849912365

1 2

预期产出:

一,

我的输出:

八,


需要帮助来调试代码并找出这种方法中的漏洞。

下面的伪代码可用于生成LCA问题的代码

///////////////////

递归调用LCA函数,一个用于左子树,一个用于右子树。如果节点为null,则只返回null,否则返回

如果node的值等于任何值v1、v2,则只需返回此节点

else递归调用子树

现在,当您收到两个递归调用的结果时,请选中->

如果两者都为null,则返回null

如果一个为null,另一个不为null,则返回非null节点


如果两者都不为空,则返回根注释本身。

您的算法无法工作

在find中,由递归调用生成的向量丢失了,因此就像不执行这些调用一样,最后只得到由初始调用生成的向量 在生命周期分析中,即使假设“发现”做了正确的事情,你也假设答案在两个向量中的相同等级,为什么? 一种方法是:

Node * Node::lca(int v1, int v2) {
  if ((data == v1) || (data == v2))
    return this;

  Node * r = (right == NULL) ? NULL : right->lca(v1, v2);
  Node * l = (left == NULL) ? NULL : left->lca(v1, v2);

  return ((r != NULL) && (l != NULL))
    ? this
    : ((r == NULL) ? l : r);
}
一个完整的例子:

#include <iostream>
#include <vector>
#include <iomanip>

class Node {
  private:
    int data;
    Node* left;
    Node* right;

  public:
    Node(int d) : data(d), left(NULL), right(NULL) {}
    void insert(int d);
    void draw(int level = 1) const;
    Node * lca(int v1, int v2);
};

void Node::insert(int d) {
  Node * n = new Node(d);
  Node * t = this;

  for (;;) {
    if (d > t->data) {
      if (t->right == NULL) {
        t->right = n;
        return;
      }
      t = t->right;
    }
    else if (t->left == NULL) {
      t->left = n;
      return;
    }
    else
      t = t->left;
  }
}

void Node::draw(int level) const {
  if (right != NULL)
    right->draw(level + 1);
  else
    std::cout << std::setw(level + 1) << '/' << std::endl;

  std::cout << std::setw(level) << data << std::endl;

  if (left != NULL)
    left->draw(level + 1);
  else
    std::cout << std::setw(level + 1) << '\\' << std::endl;
}

Node * Node::lca(int v1, int v2) {
  if ((data == v1) || (data == v2))
    return this;

  Node * r = (right == NULL) ? NULL : right->lca(v1, v2);
  Node * l = (left == NULL) ? NULL : left->lca(v1, v2);

  return ((r != NULL) && (l != NULL))
    ? this
    : ((r == NULL) ? l : r);
}

int main()
{
  Node r(8);
  std::vector<int> v = { 4, 9, 1, 2, 3, 6, 5 };

  for (auto d : v) 
    r.insert(d);

  r.draw();

  std::cout << "----------------" << std::endl;

  Node * l;

  std::cout << "lca for 1 & 2" << std::endl;
  l = r.lca(1, 2);
  if (l != NULL) 
    l->draw();

  std::cout << "----------------" << std::endl;

  std::cout << "lca for 5 & 2" << std::endl;
  l = r.lca(5, 2);
  if (l != NULL) 
    l->draw();
}
调试代码需要帮助-您拥有所有代码和测试用例。您应该使用调试器调试代码,以查看失败的地方。要求某人为你做那项工作不是应该的工作方式。
pi@raspberrypi:/tmp $ g++ -Wall lca.cpp 
pi@raspberrypi:/tmp $ ./a.out
  /
 9
  \
8
   /
  6
    /
   5
    \
 4
     /
    3
     \
   2
    \
  1
   \
----------------
lca for 1 & 2
   /
  3
   \
 2
  \
1
 \
----------------
lca for 5 & 2
  /
 6
   /
  5
   \
4
    /
   3
    \
  2
   \
 1
  \
pi@raspberrypi:/tmp $