public void HeightIterative()
{
int counter = 0;
int counter2 = 0;
TreeNode current=root;
if(current != null)
{
while(current.LeftNode!=null)
{
counter++;
current = current.LeftNode;
}
while(current.RightNode!=null)
{
counter2++;
current = current.RightNode;
}
}
int res = 1+Math.Max(counter, counter2);
Console.WriteLine("The Height Of Tree Is: "+res);
}
Я написал итерационный метод для вычисления высоты дерева. но в некоторых случаях он работает неправильно. Как в случае: 10 1 2 3 4 5 18 17 16 15 14 13 в чем проблема. согласно этой последовательности высота дерева равна 6, в то время как мой код показывает 5.