513. Find Bottom Left Tree Value
2017-04-28 23:15
148 查看
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.
Subscribe to see which companies asked this question.
Solution:
Tips:
recursion, root, right, left
Java Code:
/**
* 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) {
Queue<TreeNode> queue = new LinkedList<>();
TreeNode node = root;
queue.add(root);
while (!queue.isEmpty()) {
int queueSize = queue.size();
for (int i = 0; i < queueSize; i++) {
node = queue.poll();
if (node.right != null) {
queue.add(node.right);
}
if (node.left != null) {
queue.add(node.left);
}
}
}
return node.val;
}
}
相关文章推荐
- SQL 外链接操作小结 inner join left join right join
- 字符串查找 cmd find命令
- Jquery中find与each方法用法实例
- jQuery初学:find()方法及children方法的区别分析
- jquery计算出left和top,让一个div水平垂直居中的简单实例
- jQuery中each()、find()和filter()等节点操作方法详解(推荐)
- 谈谈Jquery中的children find 的区别有哪些
- oracle中left join和right join的区别浅谈
- 数据库中的左连接(left join)和右连接(right join)区别
- rsync结合find技巧分享
- MySQL的FIND_IN_SET函数使用方法分享
- SQL中的left join right join
- Jquery find与filter函数区别 说明
- js left,right,mid函数
- awk实现Left、join查询、去除重复值以及局部变量讲解例子
- linux find命令之exec简单概述
- linux文件搜索及其它基础命令介绍(3)
- linux find命令之xargs简单概述
- 浅谈sql连接查询的区别 inner,left,right,full
- SQL之left join、right join、inner join的区别浅析