层序遍历,保存每层的第一个值,最后的即为所求值。
class Solution {
public:int findBottomLeftValue(TreeNode* root) {
int result = root->val;
queue<TreeNode *> q;
q.push(root);
TreeNode* temp;
while (!q.empty()) {
int size = q.size();
result = q.front()->val;
while (size--) {
temp = q.front();
q.pop();
if (temp->left != NULL) q.push(temp->left);
if (temp->right != NULL) q.push(temp->right);
}
}
return result;
}
};