mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
更新 404.左叶子之和 排版格式修复
This commit is contained in:
@ -16,9 +16,9 @@
|
||||
|
||||

|
||||
|
||||
## 视频讲解
|
||||
## 算法公开课
|
||||
|
||||
**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
## 思路
|
||||
|
||||
@ -48,7 +48,7 @@ if (node->left != NULL && node->left->left == NULL && node->left->right == NULL)
|
||||
}
|
||||
```
|
||||
|
||||
## 递归法
|
||||
### 递归法
|
||||
|
||||
递归的遍历顺序为后序遍历(左右中),是因为要通过递归函数的返回值来累加求取左叶子数值之和。
|
||||
|
||||
@ -131,11 +131,11 @@ public:
|
||||
return leftValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right);
|
||||
}
|
||||
};
|
||||
```
|
||||
```
|
||||
|
||||
精简之后的代码其实看不出来用的是什么遍历方式了,对于算法初学者以上根据第一个版本来学习。
|
||||
|
||||
## 迭代法
|
||||
### 迭代法
|
||||
|
||||
本题迭代法使用前中后序都是可以的,只要把左叶子节点统计出来,就可以了,那么参考文章 [二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:迭代法统一写法](https://programmercarl.com/二叉树的统一迭代法.html)中的写法,可以写出一个前序遍历的迭代法。
|
||||
|
||||
@ -177,7 +177,7 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java
|
||||
### Java:
|
||||
|
||||
**递归**
|
||||
|
||||
@ -246,7 +246,7 @@ class Solution {
|
||||
```
|
||||
|
||||
|
||||
### Python
|
||||
### Python:
|
||||
递归
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
@ -316,7 +316,7 @@ class Solution:
|
||||
|
||||
```
|
||||
|
||||
### Go
|
||||
### Go:
|
||||
|
||||
**递归法**
|
||||
|
||||
@ -368,7 +368,7 @@ func sumOfLeftLeaves(root *TreeNode) int {
|
||||
```
|
||||
|
||||
|
||||
### JavaScript
|
||||
### JavaScript:
|
||||
|
||||
**递归法**
|
||||
|
||||
@ -417,7 +417,7 @@ var sumOfLeftLeaves = function(root) {
|
||||
};
|
||||
```
|
||||
|
||||
### TypeScript
|
||||
### TypeScript:
|
||||
|
||||
> 递归法
|
||||
|
||||
@ -462,7 +462,7 @@ function sumOfLeftLeaves(root: TreeNode | null): number {
|
||||
};
|
||||
```
|
||||
|
||||
### Swift
|
||||
### Swift:
|
||||
|
||||
**递归法**
|
||||
```swift
|
||||
@ -511,7 +511,7 @@ func sumOfLeftLeaves(_ root: TreeNode?) -> Int {
|
||||
}
|
||||
```
|
||||
|
||||
### C
|
||||
### C:
|
||||
递归法:
|
||||
```c
|
||||
int sumOfLeftLeaves(struct TreeNode* root){
|
||||
@ -561,7 +561,7 @@ int sumOfLeftLeaves(struct TreeNode* root){
|
||||
}
|
||||
```
|
||||
|
||||
### Scala
|
||||
### Scala:
|
||||
|
||||
**递归:**
|
||||
```scala
|
||||
@ -600,7 +600,7 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Rust
|
||||
### Rust:
|
||||
|
||||
**递归**
|
||||
|
||||
@ -656,3 +656,4 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user