LC45二叉树的最大深度

    科技2025-11-11  11

    2020-10-8

    这道题写了有好几遍了,再写还是为了熟悉递归的写法,熟能生巧。

    思路就是,二叉树的最大深度等于左子树的最大深度和右子树的最大深度之间的最大值加一。所以用递归求解就一目了然了。

    https://www.nowcoder.co

    /** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型 */ int maxDepth(TreeNode* root) { // write code here if(root) return max(maxDepth(root->left),maxDepth(root->right)) + 1; return 0; } };

    m/questionTerminal/8a2b2bf6c19b4f23a9bdb9b233eefa73

    Processed: 0.008, SQL: 8