Skip to content

Commit bb04684

Browse files
committed
feat: add solutions to lc problem: No.0112
No.0112.Path Sum
1 parent dde2038 commit bb04684

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

solution/0100-0199/0112.Path Sum/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,79 @@ public:
119119
};
120120
```
121121
122+
### **TypeScript**
123+
124+
```ts
125+
/**
126+
* Definition for a binary tree node.
127+
* class TreeNode {
128+
* val: number
129+
* left: TreeNode | null
130+
* right: TreeNode | null
131+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
132+
* this.val = (val===undefined ? 0 : val)
133+
* this.left = (left===undefined ? null : left)
134+
* this.right = (right===undefined ? null : right)
135+
* }
136+
* }
137+
*/
138+
139+
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
140+
if (root == null) {
141+
return false;
142+
}
143+
const { val, left, right } = root;
144+
if (left == null && right == null) {
145+
return targetSum - val === 0;
146+
}
147+
return (
148+
hasPathSum(left, targetSum - val) || hasPathSum(right, targetSum - val)
149+
);
150+
}
151+
```
152+
153+
### **Rust**
154+
155+
```rust
156+
// Definition for a binary tree node.
157+
// #[derive(Debug, PartialEq, Eq)]
158+
// pub struct TreeNode {
159+
// pub val: i32,
160+
// pub left: Option<Rc<RefCell<TreeNode>>>,
161+
// pub right: Option<Rc<RefCell<TreeNode>>>,
162+
// }
163+
//
164+
// impl TreeNode {
165+
// #[inline]
166+
// pub fn new(val: i32) -> Self {
167+
// TreeNode {
168+
// val,
169+
// left: None,
170+
// right: None
171+
// }
172+
// }
173+
// }
174+
use std::rc::Rc;
175+
use std::cell::RefCell;
176+
impl Solution {
177+
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
178+
match root {
179+
None => false,
180+
Some(node) => {
181+
let mut node = node.borrow_mut();
182+
// 确定叶结点身份
183+
if node.left.is_none() && node.right.is_none() {
184+
return target_sum - node.val == 0;
185+
}
186+
let val = node.val;
187+
Self::has_path_sum(node.left.take(), target_sum - val)
188+
|| Self::has_path_sum(node.right.take(), target_sum - val)
189+
}
190+
}
191+
}
192+
}
193+
```
194+
122195
### **...**
123196

124197
```

solution/0100-0199/0112.Path Sum/README_EN.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,79 @@ public:
107107
};
108108
```
109109
110+
### **TypeScript**
111+
112+
```ts
113+
/**
114+
* Definition for a binary tree node.
115+
* class TreeNode {
116+
* val: number
117+
* left: TreeNode | null
118+
* right: TreeNode | null
119+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
120+
* this.val = (val===undefined ? 0 : val)
121+
* this.left = (left===undefined ? null : left)
122+
* this.right = (right===undefined ? null : right)
123+
* }
124+
* }
125+
*/
126+
127+
function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
128+
if (root == null) {
129+
return false;
130+
}
131+
const { val, left, right } = root;
132+
if (left == null && right == null) {
133+
return targetSum - val === 0;
134+
}
135+
return (
136+
hasPathSum(left, targetSum - val) || hasPathSum(right, targetSum - val)
137+
);
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+
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
166+
match root {
167+
None => false,
168+
Some(node) => {
169+
let mut node = node.borrow_mut();
170+
// 确定叶结点身份
171+
if node.left.is_none() && node.right.is_none() {
172+
return target_sum - node.val == 0;
173+
}
174+
let val = node.val;
175+
Self::has_path_sum(node.left.take(), target_sum - val)
176+
|| Self::has_path_sum(node.right.take(), target_sum - val)
177+
}
178+
}
179+
}
180+
}
181+
```
182+
110183
### **...**
111184

112185
```
Lines changed: 37 additions & 0 deletions
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+
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
23+
match root {
24+
None => false,
25+
Some(node) => {
26+
let mut node = node.borrow_mut();
27+
// 确定叶结点身份
28+
if node.left.is_none() && node.right.is_none() {
29+
return target_sum - node.val == 0;
30+
}
31+
let val = node.val;
32+
Self::has_path_sum(node.left.take(), target_sum - val)
33+
|| Self::has_path_sum(node.right.take(), target_sum - val)
34+
}
35+
}
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 hasPathSum(root: TreeNode | null, targetSum: number): boolean {
16+
if (root == null) {
17+
return false;
18+
}
19+
const { val, left, right } = root;
20+
if (left == null && right == null) {
21+
return targetSum - val === 0;
22+
}
23+
return (
24+
hasPathSum(left, targetSum - val) || hasPathSum(right, targetSum - val)
25+
);
26+
}

0 commit comments

Comments
 (0)