Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e88671

Browse files
committed
feat: add solutions to lc problems: No.0101,0106,0116,0117
- No.0101.Symmetric Tree - No.0106.Construct Binary Tree from Inorder and Postorder Traversal - No.0116.Populating Next Right Pointers in Each Node - No.0117.Populating Next Right Pointers in Each Node II
1 parent ab46d51 commit 8e88671

File tree

14 files changed

+942
-41
lines changed

14 files changed

+942
-41
lines changed

‎solution/0100-0199/0101.Symmetric Tree/README.md‎

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,113 @@ public:
148148
* }
149149
*/
150150

151+
const dfs = (root1: TreeNode | null, root2: TreeNode | null) => {
152+
if (root1 == root2) {
153+
return true;
154+
}
155+
if (root1 == null || root2 == null || root1.val != root2.val) {
156+
return false;
157+
}
158+
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
159+
};
160+
151161
function isSymmetric(root: TreeNode | null): boolean {
152-
function dfs(root1, root2) {
153-
if (!root1 && !root2) return true;
154-
if (!root1 || !root2 || root1.val != root2.val) return false;
155-
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
162+
return dfs(root.left, root.right);
163+
}
164+
```
165+
166+
### **Rust**
167+
168+
```rust
169+
// Definition for a binary tree node.
170+
// #[derive(Debug, PartialEq, Eq)]
171+
// pub struct TreeNode {
172+
// pub val: i32,
173+
// pub left: Option<Rc<RefCell<TreeNode>>>,
174+
// pub right: Option<Rc<RefCell<TreeNode>>>,
175+
// }
176+
//
177+
// impl TreeNode {
178+
// #[inline]
179+
// pub fn new(val: i32) -> Self {
180+
// TreeNode {
181+
// val,
182+
// left: None,
183+
// right: None
184+
// }
185+
// }
186+
// }
187+
use std::rc::Rc;
188+
use std::cell::RefCell;
189+
impl Solution {
190+
fn dfs(root1: &Option<Rc<RefCell<TreeNode>>>, root2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
191+
if root1.is_none() && root2.is_none() {
192+
return true;
193+
}
194+
if root1.is_none() || root2.is_none() {
195+
return false;
196+
}
197+
let node1 = root1.as_ref().unwrap().borrow();
198+
let node2 = root2.as_ref().unwrap().borrow();
199+
node1.val == node2.val
200+
&& Self::dfs(&node1.left, &node2.right)
201+
&& Self::dfs(&node1.right, &node2.left)
202+
}
203+
204+
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
205+
let node = root.as_ref().unwrap().borrow();
206+
Self::dfs(&node.left, &node.right)
207+
}
208+
}
209+
```
210+
211+
```rust
212+
// Definition for a binary tree node.
213+
// #[derive(Debug, PartialEq, Eq)]
214+
// pub struct TreeNode {
215+
// pub val: i32,
216+
// pub left: Option<Rc<RefCell<TreeNode>>>,
217+
// pub right: Option<Rc<RefCell<TreeNode>>>,
218+
// }
219+
//
220+
// impl TreeNode {
221+
// #[inline]
222+
// pub fn new(val: i32) -> Self {
223+
// TreeNode {
224+
// val,
225+
// left: None,
226+
// right: None
227+
// }
228+
// }
229+
// }
230+
use std::rc::Rc;
231+
use std::cell::RefCell;
232+
use std::collections::VecDeque;
233+
impl Solution {
234+
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
235+
let root = root.unwrap();
236+
let mut node = root.as_ref().borrow_mut();
237+
let mut queue = VecDeque::new();
238+
queue.push_back([node.left.take(), node.right.take()]);
239+
while let Some([root1, root2]) = queue.pop_front() {
240+
if root1.is_none() && root2.is_none() {
241+
continue;
242+
}
243+
if root1.is_none() || root2.is_none() {
244+
return false;
245+
}
246+
if let (Some(node1), Some(node2)) = (root1, root2) {
247+
let mut node1 = node1.as_ref().borrow_mut();
248+
let mut node2 = node2.as_ref().borrow_mut();
249+
if node1.val != node2.val {
250+
return false;
251+
}
252+
queue.push_back([node1.left.take(), node2.right.take()]);
253+
queue.push_back([node1.right.take(), node2.left.take()]);
254+
}
255+
}
256+
true
156257
}
157-
return dfs(root, root);
158258
}
159259
```
160260

‎solution/0100-0199/0101.Symmetric Tree/README_EN.md‎

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,113 @@ public:
137137
* }
138138
*/
139139

140+
const dfs = (root1: TreeNode | null, root2: TreeNode | null) => {
141+
if (root1 == root2) {
142+
return true;
143+
}
144+
if (root1 == null || root2 == null || root1.val != root2.val) {
145+
return false;
146+
}
147+
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
148+
};
149+
140150
function isSymmetric(root: TreeNode | null): boolean {
141-
function dfs(root1, root2) {
142-
if (!root1 && !root2) return true;
143-
if (!root1 || !root2 || root1.val != root2.val) return false;
144-
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
151+
return dfs(root.left, root.right);
152+
}
153+
```
154+
155+
### **Rust**
156+
157+
```rust
158+
// Definition for a binary tree node.
159+
// #[derive(Debug, PartialEq, Eq)]
160+
// pub struct TreeNode {
161+
// pub val: i32,
162+
// pub left: Option<Rc<RefCell<TreeNode>>>,
163+
// pub right: Option<Rc<RefCell<TreeNode>>>,
164+
// }
165+
//
166+
// impl TreeNode {
167+
// #[inline]
168+
// pub fn new(val: i32) -> Self {
169+
// TreeNode {
170+
// val,
171+
// left: None,
172+
// right: None
173+
// }
174+
// }
175+
// }
176+
use std::rc::Rc;
177+
use std::cell::RefCell;
178+
impl Solution {
179+
fn dfs(root1: &Option<Rc<RefCell<TreeNode>>>, root2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
180+
if root1.is_none() && root2.is_none() {
181+
return true;
182+
}
183+
if root1.is_none() || root2.is_none() {
184+
return false;
185+
}
186+
let node1 = root1.as_ref().unwrap().borrow();
187+
let node2 = root2.as_ref().unwrap().borrow();
188+
node1.val == node2.val
189+
&& Self::dfs(&node1.left, &node2.right)
190+
&& Self::dfs(&node1.right, &node2.left)
191+
}
192+
193+
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
194+
let node = root.as_ref().unwrap().borrow();
195+
Self::dfs(&node.left, &node.right)
196+
}
197+
}
198+
```
199+
200+
```rust
201+
// Definition for a binary tree node.
202+
// #[derive(Debug, PartialEq, Eq)]
203+
// pub struct TreeNode {
204+
// pub val: i32,
205+
// pub left: Option<Rc<RefCell<TreeNode>>>,
206+
// pub right: Option<Rc<RefCell<TreeNode>>>,
207+
// }
208+
//
209+
// impl TreeNode {
210+
// #[inline]
211+
// pub fn new(val: i32) -> Self {
212+
// TreeNode {
213+
// val,
214+
// left: None,
215+
// right: None
216+
// }
217+
// }
218+
// }
219+
use std::rc::Rc;
220+
use std::cell::RefCell;
221+
use std::collections::VecDeque;
222+
impl Solution {
223+
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
224+
let root = root.unwrap();
225+
let mut node = root.as_ref().borrow_mut();
226+
let mut queue = VecDeque::new();
227+
queue.push_back([node.left.take(), node.right.take()]);
228+
while let Some([root1, root2]) = queue.pop_front() {
229+
if root1.is_none() && root2.is_none() {
230+
continue;
231+
}
232+
if root1.is_none() || root2.is_none() {
233+
return false;
234+
}
235+
if let (Some(node1), Some(node2)) = (root1, root2) {
236+
let mut node1 = node1.as_ref().borrow_mut();
237+
let mut node2 = node2.as_ref().borrow_mut();
238+
if node1.val != node2.val {
239+
return false;
240+
}
241+
queue.push_back([node1.left.take(), node2.right.take()]);
242+
queue.push_back([node1.right.take(), node2.left.take()]);
243+
}
244+
}
245+
true
145246
}
146-
return dfs(root, root);
147247
}
148248
```
149249

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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(root1: &Option<Rc<RefCell<TreeNode>>>, root2: &Option<Rc<RefCell<TreeNode>>>) -> bool {
23+
if root1.is_none() && root2.is_none() {
24+
return true;
25+
}
26+
if root1.is_none() || root2.is_none() {
27+
return false;
28+
}
29+
let node1 = root1.as_ref().unwrap().borrow();
30+
let node2 = root2.as_ref().unwrap().borrow();
31+
node1.val == node2.val
32+
&& Self::dfs(&node1.left, &node2.right)
33+
&& Self::dfs(&node1.right, &node2.left)
34+
}
35+
36+
pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
37+
let node = root.as_ref().unwrap().borrow();
38+
Self::dfs(&node.left, &node.right)
39+
}
40+
}

‎solution/0100-0199/0101.Symmetric Tree/Solution.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212
* }
1313
*/
1414

15-
function isSymmetric(root: TreeNode | null): boolean {
16-
function dfs(root1, root2) {
17-
if (!root1 && !root2) return true;
18-
if (!root1 || !root2 || root1.val != root2.val) return false;
19-
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
15+
const dfs = (root1: TreeNode | null, root2: TreeNode | null) => {
16+
if (root1 == root2) {
17+
return true;
18+
}
19+
if (root1 == null || root2 == null || root1.val != root2.val) {
20+
return false;
2021
}
21-
return dfs(root, root);
22+
return dfs(root1.left, root2.right) && dfs(root1.right, root2.left);
23+
};
24+
25+
function isSymmetric(root: TreeNode | null): boolean {
26+
return dfs(root.left, root.right);
2227
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /