【Breadth-first Search 】513. Find Bottom Left Tree Value
输入:一颗二叉树
输出:这颗树的最下面一层最左边的节点值。
分析:
用BFS的思路解决最直观。读每一层,在每一层记录第一个元素的值。在队列中第一层:1;第二层:2,3;第三层 4,5,6。如此继续下去。只要保留下第一个元素的值,即为答案。
分析2:
如果用DFS的思路,先序遍历树:1,2,4,3,5,7,6。遍历过程中带着当前节点的层次,每个层次只记录下遇到的第一个值。
初始化:recordLevel=0,value=0。
处理:对于节点root,层次level,如果level>recordLevellevel>recordLevellevel>recordLevel,则value=root.val,并且更新recordLevel=levelrecordLevel=levelrecordLevel=level。接着遍历左右节点。
代码
感悟:写代码就像写作一样。我们写作之前总是需要多读书,看别人怎么写,学习写作技巧。代码也一样,多看看别人写的代码,才会发现“哇,原来可以这样做啊”。看得多了,自己才可能会写。写作之前需要有独立思考,想明白要写什么,怎么写,有个大致轮廓。那会老师叫打草稿。有时候会写出来,有时候打的是腹稿。写代码也一样,写之前会先想清楚大概要怎么做,这么做是不是正确。
【Breadth-first Search 】513. Find Bottom Left Tree Value相关推荐
- 【LeetCode】513. Find Bottom Left Tree Value
问题描述 问题链接:https://leetcode.com/problems/find-bottom-left-tree-value/#/description Given a binary tre ...
- LeetCode 513. Find Bottom Left Tree Value
513. Find Bottom Left Tree Value Given a binary tree, find the leftmost value in the last row of the ...
- 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input:2/ \1 3Ou ...
- Taste/Thoth:Taste Architecture 概览【转Beyond Search】
Taste 有着非常清晰的程序架构.看图说话,下面就是一个 User-based Recommender 的例图,说明了各个组件之间的关系.而对于一个 Item-based Recommender,除 ...
- 【Full text search】检索条件具有完整的关键词布尔逻辑运算AND、OR、NOT能力
全文检索需求 1)系统提供模糊检索.分类搜索.高级复合搜索.全文检索.图片内容检索.跨库检索等多种检索途径: 2)支持字索引和词索引: 3)检索条件具有完整的关键词布尔逻辑运算AND.OR.NOT能力 ...
- Leetcode | 513. Find Bottom Left Tree Value
题目:翻转二叉树 方法①:深度优先遍历(链接) /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...
- [LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input:2/ \1 3Ou ...
- leetcode讲解--513. Find Bottom Left Tree Value
题目 Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input:2/ \1 ...
- 【mysql学习篇】为什么mysql用B+Tree?
1. 各种数据结构的对比 二叉树 不会平衡树节点,如果插入一组递增的数值,会导致树的高度很高,查询起来非常耗时 假设{1,2,3,4,5,6 } 一组数据需要存储,那么他的树的高度就为6 红黑树(二叉 ...
最新文章
- Matlab系统信息、系统命令和跨平台
- 内存区域的划分和分配
- CentOS Vi编辑器
- 中台不火,天理难容!
- 新建远程仓库并推送项目
- 【推荐算法】知识驱动的智能化推荐算法(附交流视频和PPT下载链接)
- 从浏览器输入地址到渲染出网页这个过程发生了什么?
- /proc/cpuinfo文件分析(查看CPU信息)
- 【ICLR2020】Dream to Control:Learning Behaviors by Latent Imagination
- 「Photoshop 入门教程」了解 Photoshop 工作区
- uniapp原生插件-YL视频播放器
- 计算机桌面放大了,电脑桌面好像被放大了,怎么处理?
- 怎样登陆微信公众号?怎样登陆微信公众号后台?
- 轻量级c语言开发环境,几款轻量级的C/C++编写软件
- android 广播的插件化
- matlab决策树模型过程,利用MATLAB统计工具箱进行决策树分类的一个例子
- Windows下清理maven仓库中的lastUpdated文件
- 加减法叫做什么运算_加减法四则运算的计算方法是什么
- 【C语言程序设计】实验 11
- 实验六计算机网络通信Socket编程,计算机网络socket编程实验报告(3页)-原创力文档...