mardi 20 juin 2017

Multiple or conditions within if statements

I had a basic doubt in evaluation of or conditions within a single if statement.Suppose l and r are pointers to Tree Nodes,in a scenario where either of the pointers becomes null,the check line is evaluated and false is returned. What i want to know is as either of l or r becomes null,shouldn't a null pointer exception be also raised for the third condition in the same if statement? If I place the third condition as first one an exception is raised.Can someone highlight how both the statements(second statement is mentioned as a comment) are evaluated in each case?

bool isSymmetric(TreeNode* root) {
      queue<TreeNode*>q;
      if(root==NULL)
           return true;
       q.push(root->left);
       q.push(root->right);
       TreeNode *l,*r;
       while(!q.empty())
        {
         l=q.front();
         q.pop();
         r=q.front();
         q.pop();
         if(l==NULL && r==NULL)
           continue;
         if( l==NULL || r==NULL || l->val!=r->val ) return false;//check
          //  if(  l->val!=r->val || l==NULL || r==NULL  )
          q.push(l->left);
          q.push(r->right);
          q.push(l->right);
          q.push(r->left);


            }
       return true;

Aucun commentaire:

Enregistrer un commentaire