-
Notifications
You must be signed in to change notification settings - Fork 26
/
depth-of-bt.cpp
61 lines (58 loc) · 1.57 KB
/
depth-of-bt.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
54
55
56
57
58
59
60
61
/**
* 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) {}
* };
*/
// BFS
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int count = 0;
queue <TreeNode*> q;
q.push(root);
while(!q.empty()) {
int n = q.size();
while(n--) {
TreeNode* cur = q.front();
cout << cur->val << " ";
if(cur->left != NULL) q.push(cur->left);
if(cur->right != NULL) q.push(cur->right);
q.pop();
}
count++;
}
return count;
}
};
// DFS
// public int maxDepth(TreeNode root) {
// if(root == null) {
// return 0;
// }
// Stack<TreeNode> stack = new Stack<>();
// Stack<Integer> value = new Stack<>();
// stack.push(root);
// value.push(1);
// int max = 0;
// while(!stack.isEmpty()) {
// TreeNode node = stack.pop();
// int temp = value.pop();
// max = Math.max(temp, max);
// if(node.left != null) {
// stack.push(node.left);
// value.push(temp+1);
// }
// if(node.right != null) {
// stack.push(node.right);
// value.push(temp+1);
// }
// }
// return max;
// }