/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
queue<pair<TreeNode *,int > > q;
TreeNode *tn;
int dp=0;
if(root!=NULL)
q.push(make_pair(root,1));
while(!q.empty()){
pair<TreeNode *,int> pr=q.front();
q.pop();
tn=pr.first;
dp=pr.second;
if(tn->left!=NULL){
q.push(make_pair(tn->left,dp+1));
}
if(tn->right!=NULL){
q.push(make_pair(tn->right,dp+1));
}
}
return dp;
}
};
Maximum Depth of Binary Tree
最新推荐文章于 2021-12-01 10:33:50 发布