leetcode 104. 二叉树的最大深度

 

 

/**
 * 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 == NULL)
        {
            return 0;
        }
        int left_depth = maxDepth(root->left)+1;
        int right_depth = maxDepth(root->right)+1;
        return max(left_depth,right_depth);
    }
};

 

posted @ 2021-09-26 15:25  A-inspire  Views(21)  Comments(0Edit  收藏  举报