二叉树的最大深度
- 空节点深度是 0
- 第一行是递归基
- 往左边递归,往右边递归,最大深度是取
max再加一
class Solution{public:intmaxDepth(TreeNode*root){if(root==nullptr)return0;intleftDepth=maxDepth(root->left);intrightDepth=maxDepth(root->right);returnmax(leftDepth,rightDepth)+1;}};max再加一class Solution{public:intmaxDepth(TreeNode*root){if(root==nullptr)return0;intleftDepth=maxDepth(root->left);intrightDepth=maxDepth(root->right);returnmax(leftDepth,rightDepth)+1;}};