NC13 二叉树的最大深度

题目描述:

求给定二叉树的最大深度,
最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。

解题分析:

使用递归,若node==NULL,则返回0,否则1 + max(maxDepth(root->left), maxDepth(root->right));

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

猜你喜欢

转载自blog.csdn.net/MaopengLee/article/details/118913217