Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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
Data structures C语言编程与数据结构_Data Structures - Fatal编程技术网

Data structures C语言编程与数据结构

Data structures C语言编程与数据结构,data-structures,Data Structures,如何找到二叉树两个不同节点的最近祖先?对于从左节点到根的路径上的每个节点,检查该节点是否位于从右节点到根的路径上。尝试以下操作: ances(struct tree *root, struct tree *p, struct tree *q) { struct tree *left, *right, *temp; if(root->left==p || root->right==p || root->left==q || root->righ

如何找到二叉树两个不同节点的最近祖先?

对于从左节点到根的路径上的每个节点,检查该节点是否位于从右节点到根的路径上。

尝试以下操作:

   ances(struct tree *root, struct tree *p, struct tree *q)
   {
    struct tree *left, *right, *temp;

    if(root->left==p || root->right==p || root->left==q || root->right==q)
    {
     return(root);
    }
    else
    {
     left = ancestor(root->left, p, q);
     right = ancestor(root->right, p, q);

     if(left!=NULL && right!=NULL)
     {
      return(root);
     } 
     else
     {
      temp = (left != NULL) ? left : right;
      return(temp);
     }
    }

    if(root == NULL)
      return NULL;
   }

是的,我知道怎么做了。。。谢谢