给定一个二叉树,在树的最后一行找到最左边的值。
来源:力扣(LeetCode)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int findBottomLeftValue(TreeNode* root) { queue<TreeNode*> queue; queue.push(root); while (!queue.empty()) { root = queue.front(); queue.pop(); if (root->right != nullptr) queue.push(root->right); //注意顺序, 先进先出 if (root->left != nullptr) queue.push(root->left); } return root->val; } };