Как правильно расставить метки высот вершин? - C#
Формулировка задачи:
Мне нужно каждой высоте поставить в соответствии два числа(1- высота левого поддерева, 2 - высота правого)
Написал код, но он не всегда расставляет правильно
/*private int Height(Item p)
{
if (p == null)
return 0;
int left, right;
if (p.lSon != null)
left = Height(p.lSon);
else
left = -1;
if (p.rSon != null)
right = Height(p.rSon);
else
right = -1;
int max = left > right ? left : right;
return max +1;
}*/
private int FindLeftHeight(Item p)
{
int left;
if (p == null)
return 0;
if (p.lSon != null)
left = FindLeftHeight(p.lSon);
else
left = -1;
return left + 1;
}
private int FindRightHeight(Item p)
{
int right;
if (p == null)
return 0;
if (p.rSon != null)
right = FindRightHeight(p.rSon);
else
right = -1;
return right + 1;
}Решение задачи: «Как правильно расставить метки высот вершин?»
textual
Листинг программы
public class NodeTree
{
public float ValueReal {set; get;}
public int IDnode {set; get;}
public NodeTree Lnode { set; get; }
public NodeTree Rnode { set; get; }
public NodeTree(float valueReal)
{
ValueReal = valueReal;
IDnode = 0;
}
}
public void F(NodeTree node)
{
if (node == null) { return; }
var Lval = node.Lnode != null ? 1 : 0;
var Rval = node.Rnode != null ? 1 : 0;
}