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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int findBottomLeftValue(TreeNode root) {
int left = root.val;
Queue<TreeNode> data = new LinkedList<>();
data.add(root);
while (!data.isEmpty()) {
left = data.peek().val;
int i = data.size();
while (i-- > 0) {
TreeNode temp = data.poll();
if (temp.left != null)
data.add(temp.left);
if (temp.right != null)
data.add(temp.right);
}
}
return left;
}
}