Could somebody please tell me why I'm getting W.A?


  • 0
    B

    I'm getting getting WA in the case [1,2], node with value 2, node with value 2...but when I try this case on my computer it gives me the right anwer, wich apparently does not happens for some reason, below is my code with the main function set up to this test case, so you guys can verify my answer is corret..so why am I getting W.A?

    #include<bits/stdc++.h>
    using namespace std;
    
    
    
      struct TreeNode {
          int val;
          TreeNode *left;
          TreeNode *right;
          TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      };
    vector<TreeNode*>v;
    vector<int>d;
    TreeNode *aux;
    class Solution {
    public:
        TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
                int cont = 0;
                bool flag = false;
                int r = 9999999,pos;
                gera(root,0);
                for(int i=0;i<v.size();i++)
                {
    
                  if((v[i] == p || v[i] == q) && !flag)
                    {
                        if(v[i]==p)aux = p;
                        else aux = q;
                        flag = true;
                        r = d[i];
                        pos = i;
                    }
                    else
                    {
                        if(flag && aux == p)
                        {
                            if(v[i]!=q)
                            {
                                if(d[i]<r)
                                {
                                    r = d[i];
                                    pos = i;
                                }
                            }
                            else
                            {
    
                                if(d[i]<r)
                                {
                                    r = d[i];
                                    pos = i;
                                }
                                return v[pos];
                            }
                        }
                        if(flag && aux == q)
                        {
                            if(v[i]!=p)
                            {
                                if(d[i]<r)
                                {
                                    r = d[i];
                                    pos = i;
                                }
                            }
                            else
                            {
                               if(d[i]<r)
                                {
                                    r = d[i];
                                    pos = i;
                                }
                                return v[pos];
                            }
                        }
    
    
    
                    }
    
                }
    
        }
        void gera(TreeNode* root,int cont)
        {
            if(root!=NULL)
            {
                d.push_back(cont);
                v.push_back(root);
                gera(root->left,cont+1);
                if(root->left!=NULL || root->right!=NULL)
                {
                    v.push_back(root);
                    d.push_back(cont);
                }
                gera(root->right,cont+1);
                if(root->left!=NULL || root->right!=NULL)
                {
                    v.push_back(root);
                    d.push_back(cont);
                }
    
            }
    
        }
    };
    
    int main()
    {
        Solution s;
        TreeNode *raiz = (TreeNode*)malloc(sizeof(TreeNode));
        TreeNode *raiz1 = (TreeNode*)malloc(sizeof(TreeNode));
        TreeNode *raiz2 = (TreeNode*)malloc(sizeof(TreeNode));
    
        raiz->val = 1;
        raiz1->val = 2;
    
        raiz->left = raiz1;
        raiz->right = NULL;
    
        raiz1->left = NULL;
        raiz1->right = NULL;
    
    
        cout<<s.lowestCommonAncestor(raiz,raiz1,raiz)->val<<endl;
        return 0;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.