513. Find Bottom Left Tree Value

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

Input:

    2
   / \
  1   3

Output:
1

 

Example 2: 

Input:

        1
       / \
      2   3
     /   / \
    4   5   6
       /
      7

Output:
7

 

Note: You may assume the tree (i.e., the given root node) is not NULL.

解题思路:题目不难,题意坑爹吧。还以为找的是最左下的节点,原来是找最后一排的最左节点。

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

 

posted @ 2017-02-23 22:14  Tsunami_lj  阅读(113)  评论(0编辑  收藏  举报