(二叉树 BFS) leetcode513. 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 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,也可以用DFS,不过BFS相对会简单一些的。
C++代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(!root) return ;
queue<TreeNode*> q;
q.push(root);
int res = root->val;
while(!q.empty()){
int len = q.size(); //必须加上这个,如果用i==q.size(),则会出错,因为q.size()会变化。
for(int i = len; i > ; i--){
auto t = q.front();
q.pop();
if(i == len){
res = t->val;
}
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};
(二叉树 BFS) leetcode513. Find Bottom Left Tree Value的更多相关文章
- (二叉树 BFS) leetcode993. Cousins in Binary Tree
In a binary tree, the root node is at depth 0, and children of each depth knode are at depth k+1. Tw ...
- Leetcode513. Find Bottom Left Tree Value找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- LN : leetcode 513 Find Bottom Left Tree Value
lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- 513. Find Bottom Left Tree Value - LeetCode
Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...
- [LeetCode]Find Bottom Left Tree Value
Find Bottom Left Tree Value: Given a binary tree, find the leftmost value in the last row of the tre ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
随机推荐
- MySQL 大数据量分页优化
假设有一个千万量级的表,取1到10条数据: ,; ,; 这两条语句查询时间应该在毫秒级完成: ,; 你可能没想到,这条语句执行之间在5s左右: 为什么相差这么大? 可能mysql并没有你想的那么智能, ...
- codeforces29A
Spit Problem CodeForces - 29A In a Berland's zoo there is an enclosure with camels. It is known that ...
- Linux成为云计算平台的主流操作系统
导读 这是一个人人谈"云"."大数据"的时代,作为一个IT民工,如果与同行间聊天时,不谈及这方面的内容,有人可能会觉得你落伍了,跟不上这个时代了. 这是一个人人 ...
- HBase 清空表数据
public int clearTableByTableName(String tableName) throws Exception { logger.debug("======InitH ...
- 用牛顿-拉弗森法定义平方根函数(Newton-Raphson method Square Root Python)
牛顿法(Newton’s method)又称为牛顿-拉弗森法(Newton-Raphson method),是一种近似求解实数方程式的方法.(注:Joseph Raphson在1690年出版的< ...
- ionic报错: Failed to load resource
隔了一天,才发现是代码写错了 出错的原因是在ts 文件中使用这样的定义 data: [] = ['高新区', '经开区', '其他园区']; 错误在于这个定义的类型,不能是 [],修改成 any就没有 ...
- php+redis配置
系统环境: win10+phpstudy+lamp 安装扩展 php5.6.4 =>下载地址:http://windows.php.net/downloads/pecl/releases/red ...
- Nagios 监控 Httpd 并发数插件
工作需要监控Httpd并发数,找不到合适的插件,花时间研究了一下Nagios监控内存的脚本,做了一些修改,完成了脚本.监控内存脚本:http://www.cnblogs.com/Mrhuangrui/ ...
- 洛谷P1084 疫情控制(NOIP2012)(二分答案,贪心,树形DP)
洛谷题目传送门 费了几个小时杠掉此题,如果不是那水水的数据的话,跟列队的难度真的是有得一比... 话说蒟蒻仔细翻了所有的题解,发现巨佬写的都是倍增,复杂度是\(O(n\log n\log nw)\)的 ...
- HAOI2015 简要题解
「HAOI2015」树上染色 题意 有一棵点数为 \(N\) 的树,树边有边权.给你一个在 \(0 \sim N\) 之内的正整数 \(K\),你要在这棵树中选择 \(K\) 个点,将其染成黑色,并将 ...