人防工程建设网站,flash+xml网站模板,wordpress安装步骤,做网站与平台的区别1. 题目
给定一个二叉树#xff0c;在树的最后一行找到最左边的值。
2. 解题
利用队列按层次遍历顺序#xff0c;根右左#xff0c;要求最左边的一个#xff0c;所以根右左#xff0c;最后一个队列元素就是答案 class Solution {
public:int findBottomLeftValue(TreeN…1. 题目
给定一个二叉树在树的最后一行找到最左边的值。
2. 解题
利用队列按层次遍历顺序根右左要求最左边的一个所以根右左最后一个队列元素就是答案 class Solution {
public:int findBottomLeftValue(TreeNode* root) {queueTreeNode* 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;}
};