(二叉树 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 ...
随机推荐
- 第213天:12个HTML和CSS必须知道的重点难点问题
12个HTML和CSS必须知道的重点难点问题 这12个问题,基本上就是HTML和CSS基础中的重点个难点了,也是必须要弄清楚的基本问题,其中定位的绝对定位和相对定位到底相对什么定位?这个还是容易被忽视 ...
- fftshift
说明:本文为转载http://blog.csdn.net/myathappy/article/details/51344618 Matlab fftshift 详解 一.实信号情况 因为实信号以fs为 ...
- Real-time chart using ASP.NET Core and WebSocket
Solution in glance The following diagram illustrates our solution where IoT device reports readings ...
- c++中结构体sort()排序
//添加函数头 #include <algorithm> //定义结构体Yoy typedef struct { double totalprice; //总价 doubl ...
- Linux系统下手把手完成无人值守安装服务
刚入职的运维新手经常会被要求去做一些安装操作系统的工作,如果按照用镜像光盘安装操作系统,效率会相当低下.那么如何提升效率,搭建出一套可以批量安装Linux系统的无人值守的安装系统? PXE+TFTP+ ...
- 【深入Java虚拟机】之一:Java内存模型
[深入Java虚拟机]之:Java内存区域与内存溢出 内存区域 Java虚拟机在执行Java程序的过程中会把他所管理的内存划分为若干个不同的数据区域.Java虚拟机规范将JVM所管理的内存分为以下几个 ...
- 把当前ubuntu系统做成镜像
把当前ubuntu系统做成镜像 2018年06月19日 15:24:51 还需要再学习一个 阅读数:9720 原文地址: http://community.bwbot.org/topic/167/%E ...
- 【XSY2720】区间第k小 整体二分 可持久化线段树
题目描述 给你你个序列,每次求区间第\(k\)小的数. 本题中,如果一个数在询问区间中出现了超过\(w\)次,那么就把这个数视为\(n\). 强制在线. \(n\leq 100000,a_i<n ...
- CODEFORCES掉RATING记 #5
比赛:Codeforces Round #429 (Div. 2) 时间:2017.8.1晚 这次感觉状态不好,就去打div2了 A:有\(26\)种颜色的气球,每种的数量不一样,你要把这 ...
- <Android基础>(三) UI开发 Part 1
1.常用控件 1)TextView 2)Button 3)EditText 4)ImageView 5)ProgressBar 6)AlertDialog 7)ProgressDialog 2.四种布 ...