0

This came to me while traversing a tree i wrote this:

if( node==NULL || node->left==NULL && node->left==NULL) return 1;

and the given solution had:

if(!node || !node->left && !node->right) return 1;

is there a difference between the two i know it might be something stupid but it just click right now.

On this question https://practice.geeksforgeeks.org/problems/children-sum-parent/1/?track=PC-W6-T&batchId=154

this code gives error

int isSumProperty(Node *node){
int l=0,r=0;
if( node==NULL || node->left==NULL && node->left==NULL) return 1;

if(node->left) l=node->left->data; else l=0;
if(node->right) r=node->right->data; else r=0;

return ( node->data == l+r && isSumProperty(node->left) && isSumProperty(node->right) );
}

but replace the first if statement with the other statement works can you guys explain.

the error comes with the test case: 2 N 1 N 2 N 1 N 1 N 2 N 1

ASHUTOSH SINGH
  • 131
  • 1
  • 13

2 Answers2

0

A pointer can, obviously, point at an object:

int i;
int *ip = &i;

A pointer can also be set to a "null pointer value":

int *ip = nullptr;

C and C++ also have the notion of a "null pointer constant", which is 0 used in the context of a pointer:

int *ip = 0; // null pointer constant, sets ip to hold a null pointer value

That constant is also wrapped inside a macro, for better readability:

int *ip = NULL; //null pointer constant, sets ip to hold a null pointer value

Any pointer can be tested for whether it holds a null pointer value:

int *ip = 0;
if (ip) { std::cout << "null pointer"; }

When a pointer is used in a place where a boolean value is expected, formally, it gets "contextually converted to bool". That's what happens in that if statement. The result of contextually converting a pointer to bool is false if the pointer holds a null pointer value, and true otherwise.

In the question, there are two forms of testing for a null pointer value:

if (node == NULL) // explicit test for null pointer value

if (!node) // `node` contextually converted to `bool`, result is negated

Some of the comments recommend another form:

if (node == nullptr) // explicit test for null pointer value

All three of those do the same thing. Those of us who grew up before the days of nullptr typically use if (!node). Some folks prefer to make the comparison explicit.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

On platforms where NULL can be assumed to be equivalent to 0 the !node approach can work. In most cases C++ will behave the same way, but this presumed to be platform dependent and there's no reason why NULL must be that particular value, as it could theoretically be anything you want.

In modern C++ you're encouraged to express this as if (node != nullptr) to account for this technicality and also to deal with type safety.

C programmers often use the short-hand version of !node out of habit but it is making an assumption about now NULL works. Should this assumption change in the future it's liable to break a whole lot of code like this.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Not sure what that link is about in reference to this question. – tadman Mar 21 '20 at 07:04
  • i added the whole thing will totally appreciate the help man if you looked through it i have had this problem at a few other places if you could point me to some refrence where i can read about it that would help out a lot – ASHUTOSH SINGH Mar 21 '20 at 07:08
  • That looks like another question, which is fine, but do post it as a question and not as an answer, which it isn't. Also be sure to include the error *you* are getting so we know where to start. – tadman Mar 21 '20 at 07:09
  • If `node` is a pointer, `!node` is true whenever `node` holds a null pointer vale and false otherwise. That is not platform dependent. The language definition distinguishes between a **null pointer constant** (i.e., `0` in an appropriate context) and a **null pointer value**, which is what gets stored in a pointer when you assign a null pointer constant to it. When a pointer is contextually converted to `bool`, a null pointer value gives `false` and any other value gives `true`. – Pete Becker Mar 21 '20 at 13:58
  • 1
    The key here is that, while `NULL` can be defined as anything that makes sense to the compiler, a null pointer **value** has well defined properties, regardless of the details of how it's implemented. – Pete Becker Mar 21 '20 at 14:13