LeetCode——Find Bottom Left Tree Value
Question
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.
Solution
先求出树的高度,然后层次遍历,遍历到最后一层,输出最左边的节点(也就是最后一层第一个节点,因为层次遍历采用从左到右的方式)。
Code
/**
* 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) {
int height = calcTreeHeight(root);
int count = 1;
queue<TreeNode*> childs, new_childs;
childs.push(root);
while (1) {
while(!childs.empty()) {
TreeNode* tmp = childs.front();
if (count == height)
return tmp->val;
childs.pop();
if (tmp->left != NULL)
new_childs.push(tmp->left);
if (tmp->right != NULL)
new_childs.push(tmp->right);
}
count++;
childs = new_childs;
clear(new_childs);
}
}
// 清空队列的方法
void clear( std::queue<TreeNode*> &q )
{
std::queue<TreeNode*> empty;
std::swap(q, empty );
}
int calcTreeHeight(TreeNode* root) {
if (root == NULL)
return 0;
int left = calcTreeHeight(root->left);
int right = calcTreeHeight(root->right);
return max(left, right) + 1;
}
};
Solution 2
直接层次遍历也可以,最后一层遍历结束后,直接输出最后一层的第一个节点即可。
Code 2
/**
* 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) {
queue<TreeNode*> childs, new_childs;
childs.push(root);
while (1) {
int flag = 1;
TreeNode* first;
while(!childs.empty()) {
TreeNode* tmp = childs.front();
if (flag) {
first = tmp;
flag = 0;
}
childs.pop();
if (tmp->left != NULL)
new_childs.push(tmp->left);
if (tmp->right != NULL)
new_childs.push(tmp->right);
}
if (new_childs.empty()) {
return first->val;
}
childs = new_childs;
clear(new_childs);
}
}
void clear( std::queue<TreeNode*> &q )
{
std::queue<TreeNode*> empty;
std::swap( q, empty );
}
};
LeetCode——Find Bottom Left Tree Value的更多相关文章
- [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 ...
- [LeetCode] 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 ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)
Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...
- LeetCode 513. 找树左下角的值(Find Bottom Left Tree Value)
513. 找树左下角的值 513. Find Bottom Left Tree Value 题目描述 给定一个二叉树,在树的最后一行找到最左边的值. LeetCode513. Find Bottom ...
- 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 ...
- 513. Find Bottom Left Tree Value - LeetCode
Question 513. Find Bottom Left Tree Value Solution 题目大意: 给一个二叉树,求最底层,最左侧节点的值 思路: 按层遍历二叉树,每一层第一个被访问的节 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
随机推荐
- Java8新特性 集合的stream的map
看该段代码(作用是把List中的对象替换): List<BlackMac> blackMacList = blackMacDao.queryBlackByMac(mac, (paginat ...
- 修改dedecms面包屑导航的首页链接关键字
dedecms面包屑导航默认是"主页>分类>二级分类>",我们知道链接的锚文字对排名有一定影响,这时可以考虑将“主页”改成具体的关键字,那么如何修改dedecms ...
- idea 使用方法
1:设置自定义自动补全,使用$END$代表最后光标所在的位置. http://blog.csdn.net/u012569796/article/details/54694418 2:使用 shift+ ...
- 使用java进行excel读取和写入
1:添加处理excel的依赖jar包 <!-- 引入poi,解析workbook视图 --> <dependency> <groupId>org.apache.po ...
- Linux实验楼学习之二
新建文件1-1.c touch 1-1.c 编辑文件1-1.c gedit 1-1.c 生成可执行文件1-1.c gcc -o 1-1 1-1.c 执行可执行文件1-1 ./1-1
- [LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- html select 和dropdownlist小结收集
//html select var x = $("#selectSort").val(); //获取选中的value值 获取select选中的索引: $("#selec ...
- echarts2简单笔记
1.代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...
- mysql主从延迟(摘自http://www.linuxidc.com/Linux/2012-02/53995.htm)
http://www.linuxidc.com/Linux/2012-02/53995.htm
- C#导出Excel总结
一.asp.net中导出Execl的方法:在asp.net中导出Execl有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览器 ...