求二叉树的高度(深度)

对于二叉树,树的高度和深度相等,对于某个节点来说就不一样了。

 

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

int findHeight(Node* root){
    if(root == NULL)
        return -1;
    
    return max(findHeight(root->left), findHeight(root->right)) + 1;
}

猜你喜欢

转载自blog.csdn.net/weixin_40804971/article/details/82829936
今日推荐