Skip to content

Commit 14a14e0

Browse files
committed
feat: add solutions to lc problems: No.0104, 0404
- No.0104.Maximum Depth of Binary Tree - No.0404.Sum of Left Leaves
1 parent 363a38a commit 14a14e0

File tree

8 files changed

+212
-95
lines changed

8 files changed

+212
-95
lines changed

solution/0100-0199/0104.Maximum Depth of Binary Tree/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,30 @@ impl Solution {
219219
}
220220
```
221221

222+
### **C**
223+
224+
```c
225+
/**
226+
* Definition for a binary tree node.
227+
* struct TreeNode {
228+
* int val;
229+
* struct TreeNode *left;
230+
* struct TreeNode *right;
231+
* };
232+
*/
233+
234+
#define max(a, b) (((a) > (b)) ? (a) : (b))
235+
236+
int maxDepth(struct TreeNode *root) {
237+
if (!root) {
238+
return 0;
239+
}
240+
int left = maxDepth(root->left);
241+
int right = maxDepth(root->right);
242+
return 1 + max(left, right);
243+
}
244+
```
245+
222246
### **...**
223247
224248
```

solution/0100-0199/0104.Maximum Depth of Binary Tree/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,30 @@ impl Solution {
219219
}
220220
```
221221

222+
### **C**
223+
224+
```c
225+
/**
226+
* Definition for a binary tree node.
227+
* struct TreeNode {
228+
* int val;
229+
* struct TreeNode *left;
230+
* struct TreeNode *right;
231+
* };
232+
*/
233+
234+
#define max(a, b) (((a) > (b)) ? (a) : (b))
235+
236+
int maxDepth(struct TreeNode *root) {
237+
if (!root) {
238+
return 0;
239+
}
240+
int left = maxDepth(root->left);
241+
int right = maxDepth(root->right);
242+
return 1 + max(left, right);
243+
}
244+
```
245+
222246
### **...**
223247
224248
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
#define max(a, b) (((a) > (b)) ? (a) : (b))
11+
12+
int maxDepth(struct TreeNode *root) {
13+
if (!root) {
14+
return 0;
15+
}
16+
int left = maxDepth(root->left);
17+
int right = maxDepth(root->right);
18+
return 1 + max(left, right);
19+
}

solution/0400-0499/0404.Sum of Left Leaves/README.md

+56-39
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ class Solution {
9999
}
100100
```
101101

102+
### **Go**
103+
104+
```go
105+
/**
106+
* Definition for a binary tree node.
107+
* type TreeNode struct {
108+
* Val int
109+
* Left *TreeNode
110+
* Right *TreeNode
111+
* }
112+
*/
113+
func sumOfLeftLeaves(root *TreeNode) int {
114+
if root == nil {
115+
return 0
116+
}
117+
res := 0
118+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
119+
res += root.Left.Val
120+
}
121+
res += sumOfLeftLeaves(root.Left)
122+
res += sumOfLeftLeaves(root.Right)
123+
return res
124+
}
125+
```
126+
102127
### **TypeScript**
103128

104129
```ts
@@ -117,21 +142,17 @@ class Solution {
117142
*/
118143

119144
const dfs = (root: TreeNode | null, isLeft: boolean) => {
120-
let res = 0;
145+
if (!root) {
146+
return 0;
147+
}
121148
const { val, left, right } = root;
122-
if (left == null && right == null) {
149+
if (!left && !right) {
123150
if (isLeft) {
124151
return val;
125152
}
126-
return res;
153+
return 0;
127154
}
128-
if (left != null) {
129-
res += dfs(left, true);
130-
}
131-
if (right != null) {
132-
res += dfs(right, false);
133-
}
134-
return res;
155+
return dfs(left, true) + dfs(right, false);
135156
};
136157

137158
function sumOfLeftLeaves(root: TreeNode | null): number {
@@ -164,23 +185,19 @@ use std::rc::Rc;
164185
use std::cell::RefCell;
165186
impl Solution {
166187
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, is_left: bool) -> i32 {
188+
if root.is_none() {
189+
return 0;
190+
}
167191
let node = root.as_ref().unwrap().borrow();
168192
let left = &node.left;
169193
let right = &node.right;
170-
let mut res = 0;
171194
if left.is_none() && right.is_none() {
172195
if is_left {
173196
return node.val;
174197
}
175-
return res;
176-
}
177-
if left.is_some() {
178-
res += Self::dfs(left, true);
179-
}
180-
if right.is_some() {
181-
res += Self::dfs(right, false);
198+
return 0;
182199
}
183-
res
200+
Self::dfs(left, true) + Self::dfs(right, false)
184201
}
185202

186203
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
@@ -189,30 +206,30 @@ impl Solution {
189206
}
190207
```
191208

192-
### **Go**
193-
194-
<!-- 这里可写当前语言的特殊实现逻辑 -->
209+
### **C**
195210

196-
```go
211+
```c
197212
/**
198213
* Definition for a binary tree node.
199-
* type TreeNode struct {
200-
* Val int
201-
* Left *TreeNode
202-
* Right *TreeNode
203-
* }
214+
* struct TreeNode {
215+
* int val;
216+
* struct TreeNode *left;
217+
* struct TreeNode *right;
218+
* };
204219
*/
205-
func sumOfLeftLeaves(root *TreeNode) int {
206-
if root == nil {
207-
return 0
208-
}
209-
res := 0
210-
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
211-
res += root.Left.Val
212-
}
213-
res += sumOfLeftLeaves(root.Left)
214-
res += sumOfLeftLeaves(root.Right)
215-
return res
220+
221+
int dfs(struct TreeNode *root, int isLeft) {
222+
if (!root) {
223+
return 0;
224+
}
225+
if (!root->left && !root->right) {
226+
return isLeft ? root->val : 0;
227+
}
228+
return dfs(root->left, 1) + dfs(root->right, 0);
229+
}
230+
231+
int sumOfLeftLeaves(struct TreeNode *root) {
232+
return dfs(root, 0);
216233
}
217234
```
218235

solution/0400-0499/0404.Sum of Left Leaves/README_EN.md

+56-37
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@ class Solution {
8787
}
8888
```
8989

90+
### **Go**
91+
92+
```go
93+
/**
94+
* Definition for a binary tree node.
95+
* type TreeNode struct {
96+
* Val int
97+
* Left *TreeNode
98+
* Right *TreeNode
99+
* }
100+
*/
101+
func sumOfLeftLeaves(root *TreeNode) int {
102+
if root == nil {
103+
return 0
104+
}
105+
res := 0
106+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
107+
res += root.Left.Val
108+
}
109+
res += sumOfLeftLeaves(root.Left)
110+
res += sumOfLeftLeaves(root.Right)
111+
return res
112+
}
113+
```
114+
90115
### **TypeScript**
91116

92117
```ts
@@ -105,21 +130,17 @@ class Solution {
105130
*/
106131

107132
const dfs = (root: TreeNode | null, isLeft: boolean) => {
108-
let res = 0;
133+
if (!root) {
134+
return 0;
135+
}
109136
const { val, left, right } = root;
110-
if (left == null && right == null) {
137+
if (!left && !right) {
111138
if (isLeft) {
112139
return val;
113140
}
114-
return res;
115-
}
116-
if (left != null) {
117-
res += dfs(left, true);
118-
}
119-
if (right != null) {
120-
res += dfs(right, false);
141+
return 0;
121142
}
122-
return res;
143+
return dfs(left, true) + dfs(right, false);
123144
};
124145

125146
function sumOfLeftLeaves(root: TreeNode | null): number {
@@ -152,23 +173,19 @@ use std::rc::Rc;
152173
use std::cell::RefCell;
153174
impl Solution {
154175
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, is_left: bool) -> i32 {
176+
if root.is_none() {
177+
return 0;
178+
}
155179
let node = root.as_ref().unwrap().borrow();
156180
let left = &node.left;
157181
let right = &node.right;
158-
let mut res = 0;
159182
if left.is_none() && right.is_none() {
160183
if is_left {
161184
return node.val;
162185
}
163-
return res;
164-
}
165-
if left.is_some() {
166-
res += Self::dfs(left, true);
167-
}
168-
if right.is_some() {
169-
res += Self::dfs(right, false);
186+
return 0;
170187
}
171-
res
188+
Self::dfs(left, true) + Self::dfs(right, false)
172189
}
173190

174191
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
@@ -177,28 +194,30 @@ impl Solution {
177194
}
178195
```
179196

180-
### **Go**
197+
### **C**
181198

182-
```go
199+
```c
183200
/**
184201
* Definition for a binary tree node.
185-
* type TreeNode struct {
186-
* Val int
187-
* Left *TreeNode
188-
* Right *TreeNode
189-
* }
202+
* struct TreeNode {
203+
* int val;
204+
* struct TreeNode *left;
205+
* struct TreeNode *right;
206+
* };
190207
*/
191-
func sumOfLeftLeaves(root *TreeNode) int {
192-
if root == nil {
193-
return 0
194-
}
195-
res := 0
196-
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
197-
res += root.Left.Val
198-
}
199-
res += sumOfLeftLeaves(root.Left)
200-
res += sumOfLeftLeaves(root.Right)
201-
return res
208+
209+
int dfs(struct TreeNode *root, int isLeft) {
210+
if (!root) {
211+
return 0;
212+
}
213+
if (!root->left && !root->right) {
214+
return isLeft ? root->val : 0;
215+
}
216+
return dfs(root->left, 1) + dfs(root->right, 0);
217+
}
218+
219+
int sumOfLeftLeaves(struct TreeNode *root) {
220+
return dfs(root, 0);
202221
}
203222
```
204223
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
int dfs(struct TreeNode *root, int isLeft) {
11+
if (!root) {
12+
return 0;
13+
}
14+
if (!root->left && !root->right) {
15+
return isLeft ? root->val : 0;
16+
}
17+
return dfs(root->left, 1) + dfs(root->right, 0);
18+
}
19+
20+
int sumOfLeftLeaves(struct TreeNode *root) {
21+
return dfs(root, 0);
22+
}

0 commit comments

Comments
 (0)