求树的高度

二叉树的结点定义如下:

struct BinaryTreeNode {
    int val;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
};

思路:

1,递归求出左子树和右子树高度

2,然后+1 就是树的高度

实现

int treeDepth(BinaryTreeNode* node) {
    if (node == NULL) {
        return 0;
    }
    int leftH = treeDepth(node->left);
    int rightH = treeDepth(node->right);

    if (leftH > rightH) {
        return leftH + 1;
    }
    return rightH + 1;
}
原文地址:https://www.cnblogs.com/dongma/p/13926101.html