练习题------计算一颗二叉树的高度

223 阅读1分钟

1、二叉树的高度其实就是根节点的高度。
2、高度是从当前节点开始一直到最远叶子节点,所经历的节点数量。 3、叶子节点是指没有子节点的节点。

C++实现递归

struct node
{
    int data;
    int height;
    node *lc;
    node *rc;
}

获取树的高度
返回树的高度,采用后序遍历的方式。

int get_tree_deepth(node *pnode)
{
    if (NULL ==pnode) return 0;
    
    int left_h = get_tree_deepth(pnode->lc);
    int right_h = get_tree_deepth(pnode->rc);
    
    return (left_h > right_h) ? (1+left_h) : (1+right_h);
}