Given the root
of a binary tree, return the leftmost value in the last row of the tree.
Example 1:
Input: root = [2,1,3] Output: 1
Example 2:
Input: root = [1,2,3,4,null,5,6,null,null,7] Output: 7
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. -231 <= Node.val <= 231 - 1
class Solution {
public:
int findBottomLeftValue(TreeNode *root) {
queue<TreeNode *> node_queue;
node_queue.push(root);
int bottom_left_value;
while (!node_queue.empty()) {
int layer_len = node_queue.size();
for (int i = 0; i < layer_len; i++) {
TreeNode *curr = node_queue.front();
node_queue.pop();
if (curr->left) node_queue.push(curr->left);
if (curr->right) node_queue.push(curr->right);
if (i == 0) bottom_left_value = curr->val;
}
}
return bottom_left_value;
}
};