Skip to content

Commit ba51855

Browse files
committed
feat: add solutions to lc problem: No.1022
No.1022.Sum of Root To Leaf Binary Numbers
1 parent ddd6a95 commit ba51855

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README.md

+75
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,81 @@ func sumRootToLeaf(root *TreeNode) int {
169169
}
170170
```
171171

172+
### **TypeScript**
173+
174+
```ts
175+
/**
176+
* Definition for a binary tree node.
177+
* class TreeNode {
178+
* val: number
179+
* left: TreeNode | null
180+
* right: TreeNode | null
181+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
182+
* this.val = (val===undefined ? 0 : val)
183+
* this.left = (left===undefined ? null : left)
184+
* this.right = (right===undefined ? null : right)
185+
* }
186+
* }
187+
*/
188+
189+
function sumRootToLeaf(root: TreeNode | null): number {
190+
const dfs = (root: TreeNode | null, num: number) => {
191+
if (root == null) {
192+
return 0;
193+
}
194+
const { val, left, right } = root;
195+
num = (num << 1) | val;
196+
if (left == null && right == null) {
197+
return num;
198+
}
199+
return dfs(left, num) + dfs(right, num);
200+
};
201+
return dfs(root, 0);
202+
}
203+
```
204+
205+
### **Rust**
206+
207+
```rust
208+
// Definition for a binary tree node.
209+
// #[derive(Debug, PartialEq, Eq)]
210+
// pub struct TreeNode {
211+
// pub val: i32,
212+
// pub left: Option<Rc<RefCell<TreeNode>>>,
213+
// pub right: Option<Rc<RefCell<TreeNode>>>,
214+
// }
215+
//
216+
// impl TreeNode {
217+
// #[inline]
218+
// pub fn new(val: i32) -> Self {
219+
// TreeNode {
220+
// val,
221+
// left: None,
222+
// right: None
223+
// }
224+
// }
225+
// }
226+
use std::rc::Rc;
227+
use std::cell::RefCell;
228+
impl Solution {
229+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut num: i32) -> i32 {
230+
if root.is_none() {
231+
return 0;
232+
}
233+
let root = root.as_ref().unwrap().borrow();
234+
num = (num << 1) | root.val;
235+
if root.left.is_none() && root.right.is_none() {
236+
return num;
237+
}
238+
Self::dfs(&root.left, num) + Self::dfs(&root.right, num)
239+
}
240+
241+
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
242+
Self::dfs(&root, 0)
243+
}
244+
}
245+
```
246+
172247
### **...**
173248

174249
```

solution/1000-1099/1022.Sum of Root To Leaf Binary Numbers/README_EN.md

+75
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,81 @@ func sumRootToLeaf(root *TreeNode) int {
159159
}
160160
```
161161

162+
### **TypeScript**
163+
164+
```ts
165+
/**
166+
* Definition for a binary tree node.
167+
* class TreeNode {
168+
* val: number
169+
* left: TreeNode | null
170+
* right: TreeNode | null
171+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
172+
* this.val = (val===undefined ? 0 : val)
173+
* this.left = (left===undefined ? null : left)
174+
* this.right = (right===undefined ? null : right)
175+
* }
176+
* }
177+
*/
178+
179+
function sumRootToLeaf(root: TreeNode | null): number {
180+
const dfs = (root: TreeNode | null, num: number) => {
181+
if (root == null) {
182+
return 0;
183+
}
184+
const { val, left, right } = root;
185+
num = (num << 1) | val;
186+
if (left == null && right == null) {
187+
return num;
188+
}
189+
return dfs(left, num) + dfs(right, num);
190+
};
191+
return dfs(root, 0);
192+
}
193+
```
194+
195+
### **Rust**
196+
197+
```rust
198+
// Definition for a binary tree node.
199+
// #[derive(Debug, PartialEq, Eq)]
200+
// pub struct TreeNode {
201+
// pub val: i32,
202+
// pub left: Option<Rc<RefCell<TreeNode>>>,
203+
// pub right: Option<Rc<RefCell<TreeNode>>>,
204+
// }
205+
//
206+
// impl TreeNode {
207+
// #[inline]
208+
// pub fn new(val: i32) -> Self {
209+
// TreeNode {
210+
// val,
211+
// left: None,
212+
// right: None
213+
// }
214+
// }
215+
// }
216+
use std::rc::Rc;
217+
use std::cell::RefCell;
218+
impl Solution {
219+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut num: i32) -> i32 {
220+
if root.is_none() {
221+
return 0;
222+
}
223+
let root = root.as_ref().unwrap().borrow();
224+
num = (num << 1) | root.val;
225+
if root.left.is_none() && root.right.is_none() {
226+
return num;
227+
}
228+
Self::dfs(&root.left, num) + Self::dfs(&root.right, num)
229+
}
230+
231+
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
232+
Self::dfs(&root, 0)
233+
}
234+
}
235+
```
236+
162237
### **...**
163238

164239
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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>>>, mut num: i32) -> i32 {
23+
if root.is_none() {
24+
return 0;
25+
}
26+
let root = root.as_ref().unwrap().borrow();
27+
num = (num << 1) | root.val;
28+
if root.left.is_none() && root.right.is_none() {
29+
return num;
30+
}
31+
Self::dfs(&root.left, num) + Self::dfs(&root.right, num)
32+
}
33+
34+
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
35+
Self::dfs(&root, 0)
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
function sumRootToLeaf(root: TreeNode | null): number {
16+
const dfs = (root: TreeNode | null, num: number) => {
17+
if (root == null) {
18+
return 0;
19+
}
20+
const { val, left, right } = root;
21+
num = (num << 1) | val;
22+
if (left == null && right == null) {
23+
return num;
24+
}
25+
return dfs(left, num) + dfs(right, num);
26+
};
27+
return dfs(root, 0);
28+
}

0 commit comments

Comments
 (0)