标签:ret pop str tree node ++ 递归 深度 turn bin
/**
* 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;
return max(maxDepth(root->left), maxDepth(root->right)) + 1;
}
};
/**
* 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 res = 0;
while(!q.empty())
{
int n = q.size();
while(n--)
{
TreeNode* tmp = q.front();
q.pop();
if(tmp -> left) q.push(tmp -> left);
if(tmp -> right) q.push(tmp -> right);
}
res++;
}
return res;
}
};
标签:ret pop str tree node ++ 递归 深度 turn bin
原文地址:https://www.cnblogs.com/Trevo/p/12682936.html