艰难的求职刷题之路day7:整体思路,运用队列广度优先遍历,每次便利记录最左边的值,遍历完成之后也就是最底层最左边的值啦
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*>temp;
temp.push(root);
int ans=0;
while(!temp.empty())
{
int len=temp.size();
for(int i=0;i<len;i++)
{
TreeNode *cur=temp.front();
temp.pop();
if(i==0)ans=cur->val;//记录每一层最左边的值并不断更新
if(cur->left!=nullptr)temp.push(cur->left);
if(cur->right!=nullptr)temp.push(cur->right);
}
}
return ans;
}
};