Skip to content

Commit 7d5ddb8

Browse files
committed
feat: add solutions to lcci problems: No.04.10,04.12
- No.04.10.Check SubTree - No.04.12.Paths with Sum
1 parent 344be8b commit 7d5ddb8

File tree

8 files changed

+511
-0
lines changed

8 files changed

+511
-0
lines changed

lcci/04.10.Check SubTree/README.md

+81
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,87 @@ func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
137137
}
138138
```
139139

140+
### **TypeScript**
141+
142+
```ts
143+
/**
144+
* Definition for a binary tree node.
145+
* class TreeNode {
146+
* val: number
147+
* left: TreeNode | null
148+
* right: TreeNode | null
149+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
150+
* this.val = (val===undefined ? 0 : val)
151+
* this.left = (left===undefined ? null : left)
152+
* this.right = (right===undefined ? null : right)
153+
* }
154+
* }
155+
*/
156+
157+
function checkSubTree(t1: TreeNode | null, t2: TreeNode | null): boolean {
158+
if (t1 == null && t2 == null) {
159+
return true;
160+
}
161+
if (t1 == null || t2 == null) {
162+
return false;
163+
}
164+
if (t1.val === t2.val) {
165+
return (
166+
checkSubTree(t1.left, t2.left) && checkSubTree(t1.right, t2.right)
167+
);
168+
}
169+
return checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
170+
}
171+
```
172+
173+
### **Rust**
174+
175+
```rust
176+
// Definition for a binary tree node.
177+
// #[derive(Debug, PartialEq, Eq)]
178+
// pub struct TreeNode {
179+
// pub val: i32,
180+
// pub left: Option<Rc<RefCell<TreeNode>>>,
181+
// pub right: Option<Rc<RefCell<TreeNode>>>,
182+
// }
183+
//
184+
// impl TreeNode {
185+
// #[inline]
186+
// pub fn new(val: i32) -> Self {
187+
// TreeNode {
188+
// val,
189+
// left: None,
190+
// right: None
191+
// }
192+
// }
193+
// }
194+
use std::rc::Rc;
195+
use std::cell::RefCell;
196+
impl Solution {
197+
fn dfs(t1: &Option<Rc<RefCell<TreeNode>>>, t2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
198+
if t1.is_none() && t2.is_none() {
199+
return true;
200+
}
201+
if t1.is_none() || t2.is_none() {
202+
return false;
203+
}
204+
let r1 = t1.as_ref().unwrap().borrow();
205+
let r2 = t2.as_ref().unwrap().borrow();
206+
if r1.val == r2.val {
207+
return Self::dfs(&r1.left, &r2.left) && Self::dfs(&r1.right, &r2.right);
208+
}
209+
Self::dfs(&r1.left, t2) || Self::dfs(&r1.right, t2)
210+
}
211+
212+
pub fn check_sub_tree(
213+
t1: Option<Rc<RefCell<TreeNode>>>,
214+
t2: Option<Rc<RefCell<TreeNode>>>,
215+
) -> bool {
216+
Self::dfs(&t1, &t2)
217+
}
218+
}
219+
```
220+
140221
### **...**
141222

142223
```

lcci/04.10.Check SubTree/README_EN.md

+81
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,87 @@ func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
140140
}
141141
```
142142

143+
### **TypeScript**
144+
145+
```ts
146+
/**
147+
* Definition for a binary tree node.
148+
* class TreeNode {
149+
* val: number
150+
* left: TreeNode | null
151+
* right: TreeNode | null
152+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
153+
* this.val = (val===undefined ? 0 : val)
154+
* this.left = (left===undefined ? null : left)
155+
* this.right = (right===undefined ? null : right)
156+
* }
157+
* }
158+
*/
159+
160+
function checkSubTree(t1: TreeNode | null, t2: TreeNode | null): boolean {
161+
if (t1 == null && t2 == null) {
162+
return true;
163+
}
164+
if (t1 == null || t2 == null) {
165+
return false;
166+
}
167+
if (t1.val === t2.val) {
168+
return (
169+
checkSubTree(t1.left, t2.left) && checkSubTree(t1.right, t2.right)
170+
);
171+
}
172+
return checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
173+
}
174+
```
175+
176+
### **Rust**
177+
178+
```rust
179+
// Definition for a binary tree node.
180+
// #[derive(Debug, PartialEq, Eq)]
181+
// pub struct TreeNode {
182+
// pub val: i32,
183+
// pub left: Option<Rc<RefCell<TreeNode>>>,
184+
// pub right: Option<Rc<RefCell<TreeNode>>>,
185+
// }
186+
//
187+
// impl TreeNode {
188+
// #[inline]
189+
// pub fn new(val: i32) -> Self {
190+
// TreeNode {
191+
// val,
192+
// left: None,
193+
// right: None
194+
// }
195+
// }
196+
// }
197+
use std::rc::Rc;
198+
use std::cell::RefCell;
199+
impl Solution {
200+
fn dfs(t1: &Option<Rc<RefCell<TreeNode>>>, t2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
201+
if t1.is_none() && t2.is_none() {
202+
return true;
203+
}
204+
if t1.is_none() || t2.is_none() {
205+
return false;
206+
}
207+
let r1 = t1.as_ref().unwrap().borrow();
208+
let r2 = t2.as_ref().unwrap().borrow();
209+
if r1.val == r2.val {
210+
return Self::dfs(&r1.left, &r2.left) && Self::dfs(&r1.right, &r2.right);
211+
}
212+
Self::dfs(&r1.left, t2) || Self::dfs(&r1.right, t2)
213+
}
214+
215+
pub fn check_sub_tree(
216+
t1: Option<Rc<RefCell<TreeNode>>>,
217+
t2: Option<Rc<RefCell<TreeNode>>>,
218+
) -> bool {
219+
Self::dfs(&t1, &t2)
220+
}
221+
}
222+
```
223+
143224
### **...**
144225

145226
```

lcci/04.10.Check SubTree/Solution.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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(t1: &Option<Rc<RefCell<TreeNode>>>, t2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
if t1.is_none() && t2.is_none() {
24+
return true;
25+
}
26+
if t1.is_none() || t2.is_none() {
27+
return false;
28+
}
29+
let r1 = t1.as_ref().unwrap().borrow();
30+
let r2 = t2.as_ref().unwrap().borrow();
31+
if r1.val == r2.val {
32+
return Self::dfs(&r1.left, &r2.left) && Self::dfs(&r1.right, &r2.right);
33+
}
34+
Self::dfs(&r1.left, t2) || Self::dfs(&r1.right, t2)
35+
}
36+
37+
pub fn check_sub_tree(
38+
t1: Option<Rc<RefCell<TreeNode>>>,
39+
t2: Option<Rc<RefCell<TreeNode>>>,
40+
) -> bool {
41+
Self::dfs(&t1, &t2)
42+
}
43+
}

lcci/04.10.Check SubTree/Solution.ts

+28
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 checkSubTree(t1: TreeNode | null, t2: TreeNode | null): boolean {
16+
if (t1 == null && t2 == null) {
17+
return true;
18+
}
19+
if (t1 == null || t2 == null) {
20+
return false;
21+
}
22+
if (t1.val === t2.val) {
23+
return (
24+
checkSubTree(t1.left, t2.left) && checkSubTree(t1.right, t2.right)
25+
);
26+
}
27+
return checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
28+
}

lcci/04.12.Paths with Sum/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,102 @@ class Solution {
110110
}
111111
```
112112

113+
### **TypeScript**
114+
115+
```ts
116+
/**
117+
* Definition for a binary tree node.
118+
* class TreeNode {
119+
* val: number
120+
* left: TreeNode | null
121+
* right: TreeNode | null
122+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
123+
* this.val = (val===undefined ? 0 : val)
124+
* this.left = (left===undefined ? null : left)
125+
* this.right = (right===undefined ? null : right)
126+
* }
127+
* }
128+
*/
129+
130+
function dfs(root: TreeNode | null, sum: number): number {
131+
let res = 0;
132+
if (root == null) {
133+
return res;
134+
}
135+
sum -= root.val;
136+
if (sum === 0) {
137+
res++;
138+
}
139+
return res + dfs(root.left, sum) + dfs(root.right, sum);
140+
}
141+
142+
function pathSum(root: TreeNode | null, sum: number): number {
143+
if (root == null) {
144+
return 0;
145+
}
146+
return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
147+
}
148+
```
149+
150+
### **Rust**
151+
152+
```rust
153+
// Definition for a binary tree node.
154+
// #[derive(Debug, PartialEq, Eq)]
155+
// pub struct TreeNode {
156+
// pub val: i32,
157+
// pub left: Option<Rc<RefCell<TreeNode>>>,
158+
// pub right: Option<Rc<RefCell<TreeNode>>>,
159+
// }
160+
//
161+
// impl TreeNode {
162+
// #[inline]
163+
// pub fn new(val: i32) -> Self {
164+
// TreeNode {
165+
// val,
166+
// left: None,
167+
// right: None
168+
// }
169+
// }
170+
// }
171+
use std::rc::Rc;
172+
use std::cell::RefCell;
173+
use std::collections::VecDeque;
174+
impl Solution {
175+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut sum: i32) -> i32 {
176+
let mut res = 0;
177+
if root.is_none() {
178+
return res;
179+
}
180+
let root = root.as_ref().unwrap().borrow();
181+
sum -= root.val;
182+
if sum == 0 {
183+
res += 1;
184+
}
185+
res + Self::dfs(&root.left, sum) + Self::dfs(&root.right, sum)
186+
}
187+
188+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, sum: i32) -> i32 {
189+
let mut queue = VecDeque::new();
190+
if root.is_some() {
191+
queue.push_back(root);
192+
}
193+
let mut res = 0;
194+
while let Some(mut root) = queue.pop_front() {
195+
res += Self::dfs(&root, sum);
196+
let mut root = root.as_mut().unwrap().borrow_mut();
197+
if root.left.is_some() {
198+
queue.push_back(root.left.take());
199+
}
200+
if root.right.is_some() {
201+
queue.push_back(root.right.take());
202+
}
203+
}
204+
res
205+
}
206+
}
207+
```
208+
113209
### **...**
114210

115211
```

0 commit comments

Comments
 (0)