题目
Given a binary tree, find the leftmost value in the last row of the tree.
答案
class Solution {
public int findBottomLeftValue(TreeNode root) {
if(root == null) return -1;
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root);
int candidate = -1;
while(q.size() != 0) {
int curr_size = q.size();
for(int i = 0; i < curr_size; i++) {
TreeNode t = q.poll();
if(i == 0)
candidate = t.val;
if(t.left != null) q.offer(t.left);
if(t.right != null) q.offer(t.right);
}
}
return candidate;
}
}