Segmentation fault and pointer to non existing struct is not null
<p>I have the following struct:</p>

<pre><code>typedef struct treeNode *tree;
typedef struct treeNode {
  int key; 
  tree left, right;
} treeNode;
</code></pre>

<p>with this tree:</p>

<p><a href="https://i.stack.imgur.com/fm7tb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fm7tb.png" alt="my tree"></a></p>

<p>problem:
mytree->left->left->left is NOT NULL. But why?! How Can I check if I reached the end of a branch?</p>

<pre><code>  tree mytree = (tree)malloc(sizeof(treeNode));
  mytree-&gt;key = 17;
  mytree-&gt;left = (tree)malloc(sizeof(treeNode));
  mytree-&gt;left-&gt;key = 5;
  mytree-&gt;left-&gt;left = (tree)malloc(sizeof(treeNode));
  mytree-&gt;left-&gt;right = (tree)malloc(sizeof(treeNode));
  mytree-&gt;left-&gt;left-&gt;key = 20;
  mytree-&gt;left-&gt;right-&gt;key = 2;
  mytree-&gt;right = (tree)malloc(sizeof(treeNode));
  mytree-&gt;right-&gt;key = 1;
  mytree-&gt;right-&gt;left = (tree)malloc(sizeof(treeNode));
  mytree-&gt;right-&gt;right = (tree)malloc(sizeof(treeNode));
  mytree-&gt;right-&gt;left-&gt;key = 6;
  mytree-&gt;right-&gt;right-&gt;key = 3;
</code></pre>