/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int maxDepth(struct TreeNode* root){
if(!root) {
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return (left > right ? left + 1: right+1);
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) {
return 0;
}
int ans = 0;
queue<TreeNode*> qu;
qu.push(root);
while(!qu.empty()) {
ans++;
int n = qu.size();
for(int i = 0; i < n; i++) {
TreeNode* p = qu.front();
qu.pop();
if(p->left != NULL) {
qu.push(p->left);
}
if(p->right != NULL) {
qu.push(p->right);
}
}
}
return ans;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#define MAXSIZE 10003
int maxDepth(struct TreeNode* root) {
if(root == NULL) {
return NULL;
}
int front = 0, rear = 0;
int len = 0;
struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode *) * MAXSIZE);
queue[rear++] = root;
int ans = 0;
while(front != rear) {
len = rear - front;
ans++;
while(len > 0) {
len--;
struct TreeNode* tmp = queue[front++];
if(tmp->left) {
queue[rear++] = tmp->left;
}
if(tmp->right) {
queue[rear++] = tmp->right;
}
}
}
return ans;
}
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!