这道题简单用一下层序遍历的模板便可以通过
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*>q;
if(root)q.push(root);
int k=0;
while(q.size()){
int n=q.size();
k++;
while(n--){
auto t=q.front();
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
return k;
}
};