Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:2
/ \
1 3Output:
1
Example 2:
Input:1
/ \
2 3
/ / \
4 5 6
/
7Output:
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;
}
};