首页 > 试题广场 >

设计一个函数int countHeight(BinTreeN

[问答题]

设计一个函数int countHeight(BinTreeNode *root),计算并返回一棵以root为根的二叉树的高度,(只有一个结点的二叉树高度为0),其中二叉树结点结构定义为:

typedef struct binTreeNode
{
    int data;
    struct binTreeNode *left;
    struct binTreeNode *right;
}BinTreeNode

int countHeight(BinTreeNode *root) {
    if (root == NULL) return 0;
    return max(countHeight(root.left), countHeight(root.right)) + 1;
}

发表于 2019-03-09 17:39:51 回复(0)
发表于 2018-11-03 11:18:53 回复(0)

struct Node{
Node *lc;
Node *rc;
};
int max(int a,int b){
return a>b?a:b;
}
int count(Node *x){
if(x==NULL)return 0;
return max(count(x->lc),count(x->rc))+1;
}
发表于 2017-09-07 23:05:49 回复(0)