剑指offer 55:二叉树的深度

在这里插入图片描述
思路
DFS,二叉树的后序遍历,先求左子树最大深度,再求右子树最大深度,左右子树最大深度加一即为二叉树最大深度。

class Solution {
    
    
public:
    int maxDepth(TreeNode* root) {
    
    
        if (root == nullptr) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_44537258/article/details/114016055
今日推荐