二叉树的结点定义如下:

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;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-01-30
  • 2021-12-31
  • 2021-06-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2021-04-17
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案