原题

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.

解析

查找最后一层最左侧的叶子节点的值

思路

BFS,从右向左搜索

解法(同最优解)

public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
TreeNode last = root;
queue.offer(root);
while (!queue.isEmpty()) {
last = queue.poll();
if (last.right != null) {
queue.offer(last.right);
}
if (last.left != null) {
queue.offer(last.left);
}
}
return last.val;
}

最新文章

  1. 自学H5第一天笔记
  2. Space.js – HTML 驱动的页面 3D 滚动效果
  3. RMQ(ST算法)
  4. enmo_day_07
  5. WW_TRANS_I18N_LOCALE”与“WW_TRANS_I18N_LOCALE”属性
  6. python (10) 文件夹的创建与文件夹的删除
  7. android通过httpClient请求获取JSON数据并且解析
  8. Nginx vs Apache--reference
  9. 完善GDAL与OpenCV间的数据格式转换与影像分块读写
  10. iOS中分段控制器与UIScrollView结合使用
  11. element-ui和npm、webpack、vue-cli搭建Vue项目
  12. freeMark模板引擎
  13. nethogs命令执行报异常的解决方法
  14. Servlet 2.0 &amp;&amp; Servlet 3.0 新特性
  15. L328 What Is Millennial Burnout?
  16. Visual Studio2013的安装过程及练习测试
  17. Use Memory Layout from Target Dialog Scatter File
  18. 软件产品案例分析——福州大学微信小程序
  19. alter system set events相关知识
  20. [翻译] SCRecorder

热门文章

  1. mysql.yaml
  2. win10安装MySQL 8
  3. js判断图片加载完成
  4. Sap MM 定义物料号码范围
  5. flask的信号使用
  6. json与javabean、list、map之间的转化
  7. cnbolgs博客中添加Latex支持
  8. oracle加密--wallet
  9. 18 JSON、JSON字符串、反序列化
  10. PAT(A) 1148 Werewolf - Simple Version(Java)逻辑推理