0
所以我想添加一个findHeight方法给我的教授Binary Tree Class,而且我有点麻烦。二叉树findHeight
BTNode<T> findParent(BTNode<T> Root, BTNode<T> child)
{
if(!root)
{
return Null;
}
if(Root.left* == child || Root.right* == child)
{
return Root;
}
else
{
//recursively checks left tree to see if child node is pointed to
findParent(Root.left, child);
//recursively checks right tree to see if child node is pointed to
findParent(Root.right, child);
}
int findHeight(BTNode<T> thisNode)
{
if (Count.findParent(.root, thisNode) == null)
{
return 0;
}
return findHeight(thisNode.findParent) + 1;
}
我的问题是,在findHeight方法,它调用findParent()方法,我不得不引用参数thisNode来自二进制树的根,因为这仅仅是部分上课,我不知道我应该如何引用根。 BT(二叉树)类有一个函数返回树的根,但由于我没有二进制树来引用,我不知道该怎么做。请帮忙!!!
这很有道理。对此的一个后续问题,我将如何去寻找树中每片树叶的平均高度?如果我刚刚遍历树并找到每个节点的高度,它就会按照我做它的方式工作(这当然不是最聪明的方式)。也许我读错了,但是你的函数似乎只有在你把根插入并找到整棵树的高度,而不是找到单个节点的高度时才起作用。 – user1375501
@ user1375501:至少乍一看,似乎发现树的平均高度主要涉及将两个子树的平均高度加1,不是吗? –
是的,它的确如此。感谢所有的帮助:) – user1375501