513 Find Bottom Left Tree Value 找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值。
详见:https://leetcode.com/problems/find-bottom-left-tree-value/description/
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 0;
- }
- int max_depth=1,res=root->val;
- helper(root,1,max_depth,res);
- return res;
- }
- void helper(TreeNode* node,int depth,int &max_depth,int &res)
- {
- if(!node)
- {
- return;
- }
- if(depth>max_depth)
- {
- max_depth=depth;
- res=node->val;
- }
- helper(node->left,depth+1,max_depth,res);
- helper(node->right,depth+1,max_depth,res);
- }
- };
方法二:
- /**
- * 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 0;
- }
- int res=root->val;
- queue<TreeNode*> que;
- que.push(root);
- while(!que.empty())
- {
- int n=que.size();
- for(int i=0;i<n;++i)
- {
- root=que.front();
- que.pop();
- if(i==0)
- {
- res=root->val;
- }
- if(root->left)
- {
- que.push(root->left);
- }
- if(root->right)
- {
- que.push(root->right);
- }
- }
- }
- return res;
- }
- };
参考:http://www.cnblogs.com/grandyang/p/6405128.html
513 Find Bottom Left Tree Value 找树左下角的值的更多相关文章
- Leetcode513. Find Bottom Left Tree Value找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- 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 ...
- Java实现 LeetCode 513 找树左下角的值
513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输 ...
- [Swift]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 ...
- 领扣(LeetCode)找树左下角的值 个人题解
给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输出: 7 注意: 您可以假 ...
- 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] Find Largest Value in Each Tree Row 找树每行最大的结点值
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ \ 5 3 ...
随机推荐
- TopSelf安装Windows服务提示:执行未经授权的操作。。
在一个项目中用到了八九个服务,服务的执行时间也是五花八门,有的年末执行一次,有的月中执行一次,有的月末最后一天执行一次,有的月初连续执行5天, 有的每天晚上执行,...还好各个服务并没有严格的关联关系 ...
- MapReduce算法形式三:cleanup
案例三:cleanup 其实这个案例可以不用写这么复杂,不用cleanup也能写,但是为了,突显,突显,突显(重要的事说四遍)cleanup的重要性,琢磨了半天,恩,这样写既可以突显cleanup又显 ...
- G.易彰彪的一张表
易彰彪最近有点奇怪,一向爱打游戏他最近居然盯着一张全是大小写字母的表在看,好像在找什么东西.他说,这是他女神给他的一张表,他需要回答女神的问题——在忽略大小写(即大写字母和小写字母视为同一字母)的情况 ...
- Lesson one of python
Test1:Use the powershell to output the contents print "Hello World!" print "Hello Aga ...
- dba操作之archivelog清理
下面的命令用于校验归档日志的有效性,列出无效的归档日志,以及以何种方式清除归档日志,列出几种常用的: crosscheck archivelog all; ...
- skynet源码阅读<2>--网络部分
先来看下socket_server的数据结构,这里简称为ss: struct socket_server { int recvctrl_fd; int sendctrl_fd; int checkct ...
- 汇编环境的搭建(windows 10 + debug)
1. debug.exe 安装 win10 版本过高,不再提供 debug.exe,甚至从别处获取的 debug.exe 的也无法运行. 汇编语言学习所需的各种执行文件(debug.exe.link. ...
- HDU4188:RealPhobia (连分数的运用之一)
Bert is a programmer with a real fear of floating point arithmetic. Bert has quite successfully used ...
- 【扬中集训DAY2T2】 机智的AmyZhi
[题目链接] 点击打开链接 [算法] 据说标算是暴力? 从N-200开始搜 不过我用了搜索+一些奇怪的剪枝,也A了.... [代码] 标程 #include<bits/stdc++.h> ...
- 【213】IDL函数汇总
名称 功能说明 类型 语法&举例 IDL_VALIDNAME 判断变量名是否有效,无效返回值为空或者自动修改 函数 DEFSYSV 自定义系统变量,全局变量 过程 MAKE_ARRA ...