题解 | #二叉树的最大深度#
二叉树的最大深度
https://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73
/**
* 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 == nullptr) return 0;
//左右子树的递归 到达底层返回层数+1
int ll = maxDepth(root->left);
int rr = maxDepth(root->right);
return ll > rr ? ll+1 : rr+1;
}
};
安克创新 Anker公司福利 767人发布
