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));