Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input: 2 / 1 3 Output: 1
Example 2:
Input: 1 / 2 3 / / 4 5 6 / 7 Output: 7
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int findBottomLeftValue(TreeNode* root) { 13 int rlt; 14 queue<TreeNode*> nodes; 15 nodes.push(root); 16 while (!nodes.empty()) 17 { 18 int _size = nodes.size(); 19 cout<<_size<<endl; 20 rlt = nodes.front()->val; 21 // cout<<rlt<<endl; 22 while (_size) 23 { 24 TreeNode* tmp = nodes.front(); 25 nodes.pop(); 26 if (tmp->left) 27 { 28 nodes.push(tmp->left); 29 } 30 if (tmp->right) 31 { 32 nodes.push(tmp->right); 33 } 34 _size--; 35 } 36 37 } 38 return rlt; 39 } 40 };