[LeetCode]Find Bottom Left Tree Value

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

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        if(nums.size()<3) return -1;
        int sum[nums.size()], max;
        for(int i = 0; i < nums.size(); i ++){
            i == 0?sum[i] = nums[i] : sum[i] = nums[i] + sum[i-1];
        }
        max  = sum[nums.size()-1];
        for(int i = 0; i < nums.size(); i++){
            if(max - sum[i] == sum[i] - nums[i]){
                return i;
            }
        }
        return -1;
    }
};

posted @ 2017-12-02 14:42  liangf27  阅读(67)  评论(0编辑  收藏  举报