Skip to content

Commit 4b1d191

Browse files
authored
Create main.cpp
1 parent 63dd517 commit 4b1d191

File tree

1 file changed

+36
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/03 - Maximum Depth of Binary Tree

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int maxDepth(TreeNode* root) {
15+
if(root == NULL) return 0;
16+
17+
queue<TreeNode*> q;
18+
q.push(root);
19+
int depth = 0;
20+
21+
while(!q.empty()){
22+
int levelSize = q.size();
23+
depth++;
24+
25+
for(int i = 0; i < levelSize; i++){
26+
TreeNode* temp = q.front();
27+
q.pop();
28+
29+
if(temp -> left) q.push(temp -> left);
30+
if(temp -> right) q.push(temp -> right);
31+
}
32+
}
33+
34+
return depth;
35+
}
36+
};

0 commit comments

Comments
 (0)