抱歉我的英语不好。这不是我的母语。
我的问题是我想知道一棵普通树的高度,我不知道它的英文是不是这么称呼。
树的结构是:
struct GTnode{
int data;
nodeGT *fc; //first child
nodeGT *nb; //next brother
}
接下来的每个兄弟都与第一个 child 处于同一级别,并且每个第一个 child 都有+1级别。
这是我的代码,但我不确定是否正确:
int height(GT *root){
if(root == null){
return 0;
}
else{
int max=0;
int h;
h = height(root->fc);
if(h > max){
max = h;
}
max = max + 1;
h = height(root->nb);
if(h > max){
max = h;
}
return max;
}
}
最佳答案
你的代码看起来没问题。我只是让它更紧凑一点:
#include <algorithm>
int height(GT *root) {
return root ? std::max(height(root->fc) + 1, height(root->nb)) : 0;
}
关于c++ - 普通树 - 非二叉树 - 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797604/