层序遍历二叉树,每一层深度加一
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(root==nullptr) return 0;
// int left = calculateDepth(root->left);
// int right = calculateDepth(root->right);
// return max(left,right)+1;
queue<TreeNode*> que;
que.push(root);
int depth=0;
while(!que.empty()){
int num=que.size();
while(num--){
TreeNode* node = que.front();
que.pop();
if(node->left)
que.push(node->left);
if(node->right)
que.push(node->right);
}
depth++;
}
return depth;
}
};