s4553711
4/17/2017 - 3:01 PM

513.cpp

/**
 * 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) {
        int result = root->val;
        queue<TreeNode *> q;
        q.push(root);
        TreeNode* tmp;
        while(!q.empty()) {
            int size = q.size();
            result = q.front()->val;
            while(size--) {
                tmp = q.front();
                q.pop();
                if (tmp->left != NULL) q.push(tmp->left);
                if (tmp->right != NULL) q.push(tmp->right);
            }
        }
        return result;
    }
};