This commit is contained in:
youngyangyang04
2021-08-10 21:34:26 +08:00
parent dc11080a98
commit 01972e228f
4 changed files with 107 additions and 81 deletions

View File

@@ -7,7 +7,7 @@
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
## 513.找树左下角的值
# 513.找树左下角的值
给定一个二叉树,在树的最后一行找到最左边的值。
@@ -19,7 +19,7 @@
![513.找树左下角的值1](https://img-blog.csdnimg.cn/20210204153017586.png)
## 思路
# 思路
本地要找出树的最后一行找到最左边的值。此时大家应该想起用层序遍历是非常简单的了,反而用递归的话会比较难一点。
@@ -37,7 +37,7 @@
如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。
如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/isUS-0HDYknmC0Rr4R8mww)。
如果对二叉树深度和高度还有点疑惑的话,请看:[110.平衡二叉树](https://mp.weixin.qq.com/s/7QeWnxaAB66LjFJOs40XKg)。
所以要找深度最大的叶子节点。
@@ -53,7 +53,7 @@
代码如下:
```
```C++
int maxLen = INT_MIN; // 全局变量 记录最大深度
int maxleftValue; // 全局变量 最大深度最左节点的数值
void traversal(TreeNode* root, int leftLen)
@@ -75,7 +75,7 @@ void traversal(TreeNode* root, int leftLen)
代码如下:
```
```C++
if (root->left == NULL && root->right == NULL) {
if (leftLen > maxLen) {
maxLen = leftLen; // 更新最大深度
@@ -168,7 +168,7 @@ public:
};
```
如果对回溯部分精简的代码 不理解的话,可以看这篇[二叉树:找我的所有路径](https://mp.weixin.qq.com/s/Osw4LQD2xVUnCJ-9jrYxJA)和[二叉树:以为使用了递归,其实还隐藏着回溯](https://mp.weixin.qq.com/s/ivLkHzWdhjQQD1rQWe6zWA) 。这两篇文章详细分析了回溯隐藏在了哪里。
如果对回溯部分精简的代码 不理解的话,可以看这篇[257. 二叉树的所有路径](https://mp.weixin.qq.com/s/-x0IL-5eb9W0kZC1-TM0Lw)
## 迭代法
@@ -177,7 +177,7 @@ public:
只需要记录最后一行第一个节点的数值就可以了。
如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。
如果对层序遍历不了解,看这篇[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/4-bDKi7SdwfBGRm9FYduiA),这篇里也给出了层序遍历的模板,稍作修改就一过刷了这道题了。
代码如下:
@@ -203,20 +203,20 @@ public:
};
```
## 总结
# 总结
本题涉及如下几点:
* 递归求深度的写法,我们在[110.平衡二叉树](https://mp.weixin.qq.com/s/isUS-0HDYknmC0Rr4R8mww)中详细的分析了深度应该怎么求,高度应该怎么求。
* 递归中其实隐藏了回溯,在[二叉树:以为使用了递归,其实还隐藏着回溯](https://mp.weixin.qq.com/s/ivLkHzWdhjQQD1rQWe6zWA)中讲解了究竟哪里使用了回溯,哪里隐藏了回溯。
* 层次遍历,在[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/Gb3BjakIKGNpup2jYtTzog)深度讲解了二叉树层次遍历。
* 递归求深度的写法,我们在[110.平衡二叉树](https://mp.weixin.qq.com/s/7QeWnxaAB66LjFJOs40XKg)中详细的分析了深度应该怎么求,高度应该怎么求。
* 递归中其实隐藏了回溯,在[257. 二叉树的所有路径](https://mp.weixin.qq.com/s/-x0IL-5eb9W0kZC1-TM0Lw)中讲解了究竟哪里使用了回溯,哪里隐藏了回溯。
* 层次遍历,在[二叉树:层序遍历登场!](https://mp.weixin.qq.com/s/4-bDKi7SdwfBGRm9FYduiA)深度讲解了二叉树层次遍历。
所以本题涉及到的点,我们之前都讲解过,这些知识点需要同学们灵活运用,这样就举一反三了。
## 其他语言版本
# 其他语言版本
Java
## Java
```java
// 递归法
@@ -273,9 +273,9 @@ class Solution {
Python
## Python
**递归 - 回溯**
递归:
```python
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
@@ -300,7 +300,8 @@ class Solution:
__traverse(root, 0)
return leftmost_val
```
**迭代 - 层序遍历**
迭代 - 层序遍历:
```python
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
@@ -320,19 +321,12 @@ class Solution:
queue.append(cur.right)
return result
```
Go
> 递归法
## Go
递归法:
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var maxDeep int // 全局变量 深度
var value int //全局变量 最终值
func findBottomLeftValue(root *TreeNode) int {
@@ -364,17 +358,9 @@ func findLeftValue (root *TreeNode,deep int){
}
```
> 迭代法
迭代法
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findBottomLeftValue(root *TreeNode) int {
queue:=list.New()
var gradation int
@@ -396,8 +382,10 @@ func findBottomLeftValue(root *TreeNode) int {
}
```
JavaScript:
1. 递归版本
## JavaScript
递归版本:
```javascript
var findBottomLeftValue = function(root) {
//首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可
@@ -419,7 +407,8 @@ var findBottomLeftValue = function(root) {
return resNode;
};
```
2. 层序遍历
层序遍历
```javascript
var findBottomLeftValue = function(root) {
//考虑层序遍历 记录最后一行的第一个节点