root
,请找出该二叉树的最底层最左边节点的值。
class Solution
{
public:
int findBottomLeftValue(TreeNode* root)
{
queue<TreeNode*> q;
q.push(root);
int ans;
TreeNode* tmp;
while(!q.empty())
{
int sz = q.size();
for(int i = 0; i < sz; ++i)
{
tmp = q.front(); q.pop();
if(i == 0) ans = tmp->val;
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
}
return ans;
}
};