Find Bottom Left Tree Value LeetCode solution in C++

1
class Solution {
2
public:
3
int findBottomLeftValue(TreeNode *root) {
4
queue<TreeNode *> q;
5
q.push(root);
6
int ans = root->val;
7

8
while (!q.empty()) {
9
int n = q.size();
10
for (int i = 0; i < n; i++) {
11
TreeNode *curr = q.front();
12
q.pop();
13

14
if (i == 0) ans = curr->val;
15

16
if (curr->left) q.push(curr->left);
17
if (curr->right) q.push(curr->right);
18
}
19
}
20
return ans;
21
}
22
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0