# Base condition Part 2 -11 last left node contains right node only

I am converting examples to Delphi (No good classes in Delphi) so may be slightly different.

All is fine with sample tree where all leaf’s line up even, however adding (3) to tree adds a right node to last left node, correct?

Base condition is looking for a leaf with both child nodes of nil so it fails on last left node(1) so it then calls Height on left child node that does not exist causing A/V
Have I missed something?

I ended up doing the following :

``````if IsLeaf(ANode) then
Exit(0);

if HasRightNodeOnly(ANode) then
result := 1 + HeightOfNode(ANode.Right)
else if HasLeftNodeOnly(ANode) then
result := 1 + HeightOfNode(ANode.Left)
else
result := 1 + System.Math.Max(HeightOfNode(ANode.Left),
HeightOfNode(ANode.Right));
``````

Yeah, his code has a bug. I would probably write the Java code like this:

``````if (root == null) return -1;
return 1 + Math.max(
height(root.leftChild),
height(root.rightChild));
``````

With that, the leaf nodes will get a height of 0 since the null children will both have a height of -1 and the ones with only one child will add 1 of the height of the child that is not null because the null child will have a height of -1 and the non-null child will be at least a height of 0 (so the non-null child is guaranteed to have the larger height).