/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root)
return 0;
queue<TreeNode*> q;
q.push(root);
int ans = 0;
while(q.size()){
int n = q.size();
for(int i = 0; i < n; ++i){
TreeNode* tmp = q.front();
q.pop();
if(tmp -> left)
q.push(tmp -> left);
if(tmp -> right) q.push(tmp -> right);
}
ans++;
}
return ans;
}
};
剑指 Offer 55 - I. 二叉树的深度 (入门)
最新推荐文章于 2024-07-21 22:06:29 发布