二叉树的最大深度C++

版权声明:皆为本人原创,复制必究 https://blog.csdn.net/m493096871/article/details/88824857

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root==0){
            return 0;}
        else{
            int left=maxDepth(root->left);
            int right=maxDepth(root->right);
            return max(left,right)+1;
        }
    }
};

几秒前 通过 36 ms 19.5 MB cpp

猜你喜欢

转载自blog.csdn.net/m493096871/article/details/88824857