找出完全二叉树的最近公共祖先的问题

编译通过了但是代码没有输出

代码如下:
#include
#include
using namespace std;

struct TreeNode{
    int x;
    TreeNode* left;
    TreeNode* right;
};

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){
    if(root==p||root==q||!root)return root;
       
    TreeNode* left=lowestCommonAncestor(root->left,p,q);
    TreeNode* right=lowestCommonAncestor(root->right,p,q);
       
    if(!left&&!right)return NULL;
    else if(left&&!right)return left;
    else if(right&&!left)return right;
       
    return root;
}

TreeNode* Search(TreeNode* &root,int i){
    if((root->x)==i){
        return root;
    }
    Search(root->left,i);
    Search(root->right,i);
}

void CreateTree(TreeNode* &root,int a,int len,int loc)
{
    if(loc>=len){root=NULL;return;}
    root->x=a[loc];
    CreateTree(root->left,a,len,2
loc+1);
    CreateTree(root->right,a,len,2*(loc+1));
}

int main(){
    TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
    int a[1000];
    for(int i=0;i<1000;i++){
        a[i]=i+1;
    }
    CreateTree(root,a,999,0);
    int m,n,Y;
    cin>>Y;
    for(int i=0;i<Y;i++){
        cin>>m>>n;
        TreeNode* p=Search(root,m);
        TreeNode* q=Search(root,n);
        TreeNode* r=lowestCommonAncestor(root,p,q);
        cout<x<<endl;
    }
}

求大神解答

这个search函数有问题

TreeNode* Search(TreeNode* &root,int i){
    if((root->x)==i){
        return root;
    }
    TreeNode *pNode = NULL;
    if(root->left != NULL)
    {
            pNode = Search(root->left,i);
            if(pNode != NULL)
                  return pNode;
     }
    if(root->right != NULL)
    {
          pNode = Search(root->right,i);
          if(pNode != NULL)
                  return pNode;
    }
    return NULL;
}