-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104&&111.cpp
53 lines (48 loc) · 1.08 KB
/
104&&111.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int maxDepth(TreeNode* root){
//first one dfs
//return root==nullptr? 0:max(maxDepth(root->left),maxDepth(root->right))+1;
//second one bfs
if(root==nullptr)return 0;
queue<TreeNode*>q;
int ret=0;
q.push(root);
while(!q.empty()){
++ret;
for(int i=0,n=q.size();i<n;++i){
auto t=q.front();
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
}
}
return ret;
}
int minDepth(TreeNode* root){
if(root==nullptr)return 0;
queue<TreeNode*>q;
int ret=0;
q.push(root);
while(!q.empty()){
++ret;
for(int i=0,n=q.size();i<n;++i){
auto t=q.front();
q.pop();
if(t->left)q.push(t->left);
if(t->right)q.push(t->right);
if(t->right==nullptr && t->left==nullptr)return ret;
}
}
return ret;
}
int main(){
return 0;
}