Skip to content

Commit 2ff09a5

Browse files
committed
feat: add solutions to lc problems: No.0404,1290
- No.0404.Sum of Left Leaves - No.1290.Convert Binary Number in a Linked List to Integer
1 parent af57149 commit 2ff09a5

File tree

7 files changed

+354
-0
lines changed

7 files changed

+354
-0
lines changed

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,96 @@ class Solution {
9898
}
9999
```
100100

101+
### **TypeScript**
102+
103+
```ts
104+
/**
105+
* Definition for a binary tree node.
106+
* class TreeNode {
107+
* val: number
108+
* left: TreeNode | null
109+
* right: TreeNode | null
110+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
111+
* this.val = (val===undefined ? 0 : val)
112+
* this.left = (left===undefined ? null : left)
113+
* this.right = (right===undefined ? null : right)
114+
* }
115+
* }
116+
*/
117+
118+
const dfs = (root: TreeNode | null, isLeft: boolean) => {
119+
let res = 0;
120+
const { val, left, right } = root;
121+
if (left == null && right == null) {
122+
if (isLeft) {
123+
return val;
124+
}
125+
return res;
126+
}
127+
if (left != null) {
128+
res += dfs(left, true);
129+
}
130+
if (right != null) {
131+
res += dfs(right, false);
132+
}
133+
return res;
134+
};
135+
136+
function sumOfLeftLeaves(root: TreeNode | null): number {
137+
return dfs(root, false);
138+
}
139+
```
140+
141+
### **Rust**
142+
143+
```rust
144+
// Definition for a binary tree node.
145+
// #[derive(Debug, PartialEq, Eq)]
146+
// pub struct TreeNode {
147+
// pub val: i32,
148+
// pub left: Option<Rc<RefCell<TreeNode>>>,
149+
// pub right: Option<Rc<RefCell<TreeNode>>>,
150+
// }
151+
//
152+
// impl TreeNode {
153+
// #[inline]
154+
// pub fn new(val: i32) -> Self {
155+
// TreeNode {
156+
// val,
157+
// left: None,
158+
// right: None
159+
// }
160+
// }
161+
// }
162+
use std::rc::Rc;
163+
use std::cell::RefCell;
164+
impl Solution {
165+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, is_left: bool) -> i32 {
166+
let node = root.as_ref().unwrap().borrow();
167+
let left = &node.left;
168+
let right = &node.right;
169+
let mut res = 0;
170+
if left.is_none() && right.is_none() {
171+
if is_left {
172+
return node.val;
173+
}
174+
return res;
175+
}
176+
if left.is_some() {
177+
res += Self::dfs(left, true);
178+
}
179+
if right.is_some() {
180+
res += Self::dfs(right, false);
181+
}
182+
res
183+
}
184+
185+
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
186+
Self::dfs(&root, false)
187+
}
188+
}
189+
```
190+
101191
### **...**
102192

103193
```

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,96 @@ class Solution {
8484
}
8585
```
8686

87+
### **TypeScript**
88+
89+
```ts
90+
/**
91+
* Definition for a binary tree node.
92+
* class TreeNode {
93+
* val: number
94+
* left: TreeNode | null
95+
* right: TreeNode | null
96+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
97+
* this.val = (val===undefined ? 0 : val)
98+
* this.left = (left===undefined ? null : left)
99+
* this.right = (right===undefined ? null : right)
100+
* }
101+
* }
102+
*/
103+
104+
const dfs = (root: TreeNode | null, isLeft: boolean) => {
105+
let res = 0;
106+
const { val, left, right } = root;
107+
if (left == null && right == null) {
108+
if (isLeft) {
109+
return val;
110+
}
111+
return res;
112+
}
113+
if (left != null) {
114+
res += dfs(left, true);
115+
}
116+
if (right != null) {
117+
res += dfs(right, false);
118+
}
119+
return res;
120+
};
121+
122+
function sumOfLeftLeaves(root: TreeNode | null): number {
123+
return dfs(root, false);
124+
}
125+
```
126+
127+
### **Rust**
128+
129+
```rust
130+
// Definition for a binary tree node.
131+
// #[derive(Debug, PartialEq, Eq)]
132+
// pub struct TreeNode {
133+
// pub val: i32,
134+
// pub left: Option<Rc<RefCell<TreeNode>>>,
135+
// pub right: Option<Rc<RefCell<TreeNode>>>,
136+
// }
137+
//
138+
// impl TreeNode {
139+
// #[inline]
140+
// pub fn new(val: i32) -> Self {
141+
// TreeNode {
142+
// val,
143+
// left: None,
144+
// right: None
145+
// }
146+
// }
147+
// }
148+
use std::rc::Rc;
149+
use std::cell::RefCell;
150+
impl Solution {
151+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, is_left: bool) -> i32 {
152+
let node = root.as_ref().unwrap().borrow();
153+
let left = &node.left;
154+
let right = &node.right;
155+
let mut res = 0;
156+
if left.is_none() && right.is_none() {
157+
if is_left {
158+
return node.val;
159+
}
160+
return res;
161+
}
162+
if left.is_some() {
163+
res += Self::dfs(left, true);
164+
}
165+
if right.is_some() {
166+
res += Self::dfs(right, false);
167+
}
168+
res
169+
}
170+
171+
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
172+
Self::dfs(&root, false)
173+
}
174+
}
175+
```
176+
87177
### **...**
88178

89179
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, is_left: bool) -> i32 {
23+
let node = root.as_ref().unwrap().borrow();
24+
let left = &node.left;
25+
let right = &node.right;
26+
let mut res = 0;
27+
if left.is_none() && right.is_none() {
28+
if is_left {
29+
return node.val;
30+
}
31+
return res;
32+
}
33+
if left.is_some() {
34+
res += Self::dfs(left, true);
35+
}
36+
if right.is_some() {
37+
res += Self::dfs(right, false);
38+
}
39+
res
40+
}
41+
42+
pub fn sum_of_left_leaves(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
43+
Self::dfs(&root, false)
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
const dfs = (root: TreeNode | null, isLeft: boolean) => {
16+
let res = 0;
17+
const { val, left, right } = root;
18+
if (left == null && right == null) {
19+
if (isLeft) {
20+
return val;
21+
}
22+
return res;
23+
}
24+
if (left != null) {
25+
res += dfs(left, true);
26+
}
27+
if (right != null) {
28+
res += dfs(right, false);
29+
}
30+
return res;
31+
};
32+
33+
function sumOfLeftLeaves(root: TreeNode | null): number {
34+
return dfs(root, false);
35+
}

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,39 @@ public:
158158
};
159159
```
160160
161+
### **Rust**
162+
163+
```rust
164+
// Definition for singly-linked list.
165+
// #[derive(PartialEq, Eq, Clone, Debug)]
166+
// pub struct ListNode {
167+
// pub val: i32,
168+
// pub next: Option<Box<ListNode>>
169+
// }
170+
//
171+
// impl ListNode {
172+
// #[inline]
173+
// fn new(val: i32) -> Self {
174+
// ListNode {
175+
// next: None,
176+
// val
177+
// }
178+
// }
179+
// }
180+
impl Solution {
181+
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
182+
let mut cur = &head;
183+
let mut res = 0;
184+
while let Some(node) = cur {
185+
res <<= 1;
186+
res |= node.val;
187+
cur = &node.next;
188+
}
189+
res
190+
}
191+
}
192+
```
193+
161194
### **...**
162195

163196
```

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,39 @@ public:
126126
};
127127
```
128128
129+
### **Rust**
130+
131+
```rust
132+
// Definition for singly-linked list.
133+
// #[derive(PartialEq, Eq, Clone, Debug)]
134+
// pub struct ListNode {
135+
// pub val: i32,
136+
// pub next: Option<Box<ListNode>>
137+
// }
138+
//
139+
// impl ListNode {
140+
// #[inline]
141+
// fn new(val: i32) -> Self {
142+
// ListNode {
143+
// next: None,
144+
// val
145+
// }
146+
// }
147+
// }
148+
impl Solution {
149+
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
150+
let mut cur = &head;
151+
let mut res = 0;
152+
while let Some(node) = cur {
153+
res <<= 1;
154+
res |= node.val;
155+
cur = &node.next;
156+
}
157+
res
158+
}
159+
}
160+
```
161+
129162
### **...**
130163

131164
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
19+
let mut cur = &head;
20+
let mut res = 0;
21+
while let Some(node) = cur {
22+
res <<= 1;
23+
res |= node.val;
24+
cur = &node.next;
25+
}
26+
res
27+
}
28+
}

0 commit comments

Comments
 (0)