力扣[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) return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
        //左儿子和右儿子的最大值+1 就是深度
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_45488131/article/details/108720156
今日推荐