1. 题目
给定一个二叉树,在树的最后一行找到最左边的值。
2. 解题
- 利用队列按层次遍历
- 顺序,根右左,要求最左边的一个,所以根右左,最后一个队列元素就是答案
class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> q;q.push(root);int ans;TreeNode *temp;while (!q.empty()) {temp = q.front();ans = temp->val;q.pop();if(temp->right)q.push(temp->right);if(temp->left)q.push(temp->left);}return ans;}
};