寻找一棵树的深度? [英] finding depth of a tree?
本文介绍了寻找一棵树的深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对二叉树和递归非常陌生。我的程序是找到树的高度,但是我对我的程序为什么不起作用感到困惑。
I am very new to binary tree and recursion. My program is to find the height of the tree but I am a bit confused as to why my program doesn't work.
struct Node {
int value;
Node *left;
Node *right;
}
int heightOfTree(Node node){
if(node ==NULL)
{
return 0;
}
else
{
int lheight=heightOfTree(node->left);
int rheight = heightOfTree(node->right);
if(lheight>rheight)
{
return lheight;
}
else
{
return rheight;
}
}
}
我在网上跟踪了伪代码,因此我自己实现了它,因为我不想只复制粘贴。我试图插入很多节点,但是当我运行程序时,我总是得到0高度吗?谢谢
I followed a pseudocode online so I implemented it myself because I don't want to just copy and paste. I tried to insert a lot of nodes but When I run my program I always get 0 height? Thank you
推荐答案
return lheight + 1;
和
return rheight + 1;
您需要在每个级别上增加高度。
You need to increment the height at each level.
这篇关于寻找一棵树的深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文