-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-depth-of-binary-tree.cpp
More file actions
68 lines (62 loc) · 1.62 KB
/
maximum-depth-of-binary-tree.cpp
File metadata and controls
68 lines (62 loc) · 1.62 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* 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:
//迭代做法2 BFS
int maxDepth(TreeNode* root) {
if(!root) return 0;
queue<TreeNode*> q; // 按层存储
q.emplace(root);
int mx=0;
while(!q.empty()){
int size=q.size(); //先记录本层有多少个节点
while(--size>=0){ //while(size-->0){
TreeNode* n=q.front();
q.pop();
if(n->left) q.emplace(n->left);
if(n->right) q.emplace(n->right);
}
mx++;
}
return mx;
}
//迭代做法1 DFS
int maxDepth2(TreeNode* root) {
if(!root) return 0;
int mx=INT_MIN;
stack<TreeNode*> sn;
stack<int> si;
sn.emplace(root);
si.emplace(1);
while(!sn.empty()){
TreeNode* n=sn.top();
int len=si.top();
sn.pop();
si.pop();
mx=max(mx,len);
if(n->left){
sn.emplace(n->left);
si.emplace(len+1);
}
if(n->right){
sn.emplace(n->right);
si.emplace(len+1);
}
}
return mx;
}
//递归做法
int maxDepth1(TreeNode* root) {
if(!root) return 0;
if(!root->left && !root->right) return 1;
int res=max(maxDepth(root->left),maxDepth(root->right))+1;
return res;
}
};