Case 1:
struct node *root = (struct node *) malloc(sizeof(struct node)); assert(root->left == NULL); assert(root->right == NULL);
This is an accident and usually fails. There is no guarantee that the malloced memory from the heap will be reset - you need a calloc for this. However, many of the debug time libraries I've seen will have zero allocated memory, so I can assume that this works for you in debug mode. This may vary between debug and release modes.
Case 2:
static struct node root2;
By default, global variables are nullified. I think this is guaranteed behavior, so that is correct.
Case 3:
struct node root1; assert(root1.left == NULL); assert(root1.right == NULL);
This is allocated on the stack and remains uninitialized. Again, there is no guarantee on what values you will stay here, so you expect this to fail.
source share