update 0404

This commit is contained in:
Chengkq
2021-05-13 13:30:55 +08:00
parent 1a6d00319d
commit dc2b5281e6
3 changed files with 56 additions and 5 deletions

View File

@ -253,9 +253,13 @@ public:
## 其他语言版本
Java
```java
```
Python

View File

@ -353,9 +353,13 @@ public:
## 其他语言版本
Java
```java
```
Python

View File

@ -159,9 +159,51 @@ public:
## 其他语言版本
Java
**递归**
```java
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int leftValue = sumOfLeftLeaves(root.left); // 左
int rightValue = sumOfLeftLeaves(root.right); // 右
int midValue = 0;
if (root.left != null && root.left.left == null && root.left.right == null) { // 中
midValue = root.left.val;
}
int sum = midValue + leftValue + rightValue;
return sum;
}
}
```
**迭代**
```java
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
Stack<TreeNode> stack = new Stack<> ();
stack.add(root);
int result = 0;
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null && node.left.left == null && node.left.right == null) {
result += node.left.val;
}
if (node.right != null) stack.add(node.right);
if (node.left != null) stack.add(node.left);
}
return result;
}
}
```
Python
@ -176,3 +218,4 @@ Go
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>