Skip to content

Commit 0e604da

Browse files
committed
feat: add solutions to lc problem: No.2196
No.2196.Create Binary Tree From Descriptions
1 parent 58e908a commit 0e604da

File tree

4 files changed

+320
-0
lines changed

4 files changed

+320
-0
lines changed

solution/2100-2199/2196.Create Binary Tree From Descriptions/README.md

+108
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,115 @@
7777
### **TypeScript**
7878

7979
```ts
80+
/**
81+
* Definition for a binary tree node.
82+
* class TreeNode {
83+
* val: number
84+
* left: TreeNode | null
85+
* right: TreeNode | null
86+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
87+
* this.val = (val===undefined ? 0 : val)
88+
* this.left = (left===undefined ? null : left)
89+
* this.right = (right===undefined ? null : right)
90+
* }
91+
* }
92+
*/
93+
94+
function createBinaryTree(descriptions: number[][]): TreeNode | null {
95+
const map = new Map<number, [number, number]>();
96+
const isRoot = new Map<number, boolean>();
97+
for (const [parent, child, isLeft] of descriptions) {
98+
let [left, right] = map.get(parent) ?? [0, 0];
99+
if (isLeft) {
100+
left = child;
101+
} else {
102+
right = child;
103+
}
104+
if (!isRoot.has(parent)) {
105+
isRoot.set(parent, true);
106+
}
107+
isRoot.set(child, false);
108+
map.set(parent, [left, right]);
109+
}
110+
const dfs = (val: number) => {
111+
if (val === 0) {
112+
return null;
113+
}
114+
const [left, right] = map.get(val) ?? [0, 0];
115+
return new TreeNode(val, dfs(left), dfs(right));
116+
};
117+
for (const [key, val] of isRoot.entries()) {
118+
if (val) {
119+
return dfs(key);
120+
}
121+
}
122+
return null;
123+
}
124+
```
80125

126+
### **Rust**
127+
128+
```rust
129+
// Definition for a binary tree node.
130+
// #[derive(Debug, PartialEq, Eq)]
131+
// pub struct TreeNode {
132+
// pub val: i32,
133+
// pub left: Option<Rc<RefCell<TreeNode>>>,
134+
// pub right: Option<Rc<RefCell<TreeNode>>>,
135+
// }
136+
//
137+
// impl TreeNode {
138+
// #[inline]
139+
// pub fn new(val: i32) -> Self {
140+
// TreeNode {
141+
// val,
142+
// left: None,
143+
// right: None
144+
// }
145+
// }
146+
// }
147+
use std::rc::Rc;
148+
use std::cell::RefCell;
149+
use std::collections::HashMap;
150+
impl Solution {
151+
fn dfs(val: i32, map: &HashMap<i32, [i32; 2]>) -> Option<Rc<RefCell<TreeNode>>> {
152+
if val == 0 {
153+
return None;
154+
}
155+
let mut left = None;
156+
let mut right = None;
157+
if let Some(&[l_val, r_val]) = map.get(&val) {
158+
left = Self::dfs(l_val, map);
159+
right = Self::dfs(r_val, map);
160+
}
161+
Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
162+
}
163+
164+
pub fn create_binary_tree(descriptions: Vec<Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
165+
let mut map = HashMap::new();
166+
let mut is_root = HashMap::new();
167+
for description in descriptions.iter() {
168+
let (parent, child, is_left) = (description[0], description[1], description[2] == 1);
169+
let [mut left, mut right] = map.get(&parent).unwrap_or(&[0, 0]);
170+
if is_left {
171+
left = child;
172+
} else {
173+
right = child;
174+
}
175+
if !is_root.contains_key(&parent) {
176+
is_root.insert(parent, true);
177+
}
178+
is_root.insert(child, false);
179+
map.insert(parent, [left, right]);
180+
}
181+
for key in is_root.keys() {
182+
if *is_root.get(key).unwrap() {
183+
return Self::dfs(*key, &map);
184+
}
185+
}
186+
None
187+
}
188+
}
81189
```
82190

83191
### **...**

solution/2100-2199/2196.Create Binary Tree From Descriptions/README_EN.md

+108
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,115 @@ The resulting binary tree is shown in the diagram.
6464
### **TypeScript**
6565

6666
```ts
67+
/**
68+
* Definition for a binary tree node.
69+
* class TreeNode {
70+
* val: number
71+
* left: TreeNode | null
72+
* right: TreeNode | null
73+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
74+
* this.val = (val===undefined ? 0 : val)
75+
* this.left = (left===undefined ? null : left)
76+
* this.right = (right===undefined ? null : right)
77+
* }
78+
* }
79+
*/
80+
81+
function createBinaryTree(descriptions: number[][]): TreeNode | null {
82+
const map = new Map<number, [number, number]>();
83+
const isRoot = new Map<number, boolean>();
84+
for (const [parent, child, isLeft] of descriptions) {
85+
let [left, right] = map.get(parent) ?? [0, 0];
86+
if (isLeft) {
87+
left = child;
88+
} else {
89+
right = child;
90+
}
91+
if (!isRoot.has(parent)) {
92+
isRoot.set(parent, true);
93+
}
94+
isRoot.set(child, false);
95+
map.set(parent, [left, right]);
96+
}
97+
const dfs = (val: number) => {
98+
if (val === 0) {
99+
return null;
100+
}
101+
const [left, right] = map.get(val) ?? [0, 0];
102+
return new TreeNode(val, dfs(left), dfs(right));
103+
};
104+
for (const [key, val] of isRoot.entries()) {
105+
if (val) {
106+
return dfs(key);
107+
}
108+
}
109+
return null;
110+
}
111+
```
67112

113+
### **Rust**
114+
115+
```rust
116+
// Definition for a binary tree node.
117+
// #[derive(Debug, PartialEq, Eq)]
118+
// pub struct TreeNode {
119+
// pub val: i32,
120+
// pub left: Option<Rc<RefCell<TreeNode>>>,
121+
// pub right: Option<Rc<RefCell<TreeNode>>>,
122+
// }
123+
//
124+
// impl TreeNode {
125+
// #[inline]
126+
// pub fn new(val: i32) -> Self {
127+
// TreeNode {
128+
// val,
129+
// left: None,
130+
// right: None
131+
// }
132+
// }
133+
// }
134+
use std::rc::Rc;
135+
use std::cell::RefCell;
136+
use std::collections::HashMap;
137+
impl Solution {
138+
fn dfs(val: i32, map: &HashMap<i32, [i32; 2]>) -> Option<Rc<RefCell<TreeNode>>> {
139+
if val == 0 {
140+
return None;
141+
}
142+
let mut left = None;
143+
let mut right = None;
144+
if let Some(&[l_val, r_val]) = map.get(&val) {
145+
left = Self::dfs(l_val, map);
146+
right = Self::dfs(r_val, map);
147+
}
148+
Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
149+
}
150+
151+
pub fn create_binary_tree(descriptions: Vec<Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
152+
let mut map = HashMap::new();
153+
let mut is_root = HashMap::new();
154+
for description in descriptions.iter() {
155+
let (parent, child, is_left) = (description[0], description[1], description[2] == 1);
156+
let [mut left, mut right] = map.get(&parent).unwrap_or(&[0, 0]);
157+
if is_left {
158+
left = child;
159+
} else {
160+
right = child;
161+
}
162+
if !is_root.contains_key(&parent) {
163+
is_root.insert(parent, true);
164+
}
165+
is_root.insert(child, false);
166+
map.insert(parent, [left, right]);
167+
}
168+
for key in is_root.keys() {
169+
if *is_root.get(key).unwrap() {
170+
return Self::dfs(*key, &map);
171+
}
172+
}
173+
None
174+
}
175+
}
68176
```
69177

70178
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
use std::collections::HashMap;
22+
impl Solution {
23+
fn dfs(val: i32, map: &HashMap<i32, [i32; 2]>) -> Option<Rc<RefCell<TreeNode>>> {
24+
if val == 0 {
25+
return None;
26+
}
27+
let mut left = None;
28+
let mut right = None;
29+
if let Some(&[l_val, r_val]) = map.get(&val) {
30+
left = Self::dfs(l_val, map);
31+
right = Self::dfs(r_val, map);
32+
}
33+
Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
34+
}
35+
36+
pub fn create_binary_tree(descriptions: Vec<Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
37+
let mut map = HashMap::new();
38+
let mut is_root = HashMap::new();
39+
for description in descriptions.iter() {
40+
let (parent, child, is_left) = (description[0], description[1], description[2] == 1);
41+
let [mut left, mut right] = map.get(&parent).unwrap_or(&[0, 0]);
42+
if is_left {
43+
left = child;
44+
} else {
45+
right = child;
46+
}
47+
if !is_root.contains_key(&parent) {
48+
is_root.insert(parent, true);
49+
}
50+
is_root.insert(child, false);
51+
map.insert(parent, [left, right]);
52+
}
53+
for key in is_root.keys() {
54+
if *is_root.get(key).unwrap() {
55+
return Self::dfs(*key, &map);
56+
}
57+
}
58+
None
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 createBinaryTree(descriptions: number[][]): TreeNode | null {
16+
const map = new Map<number, [number, number]>();
17+
const isRoot = new Map<number, boolean>();
18+
for (const [parent, child, isLeft] of descriptions) {
19+
let [left, right] = map.get(parent) ?? [0, 0];
20+
if (isLeft) {
21+
left = child;
22+
} else {
23+
right = child;
24+
}
25+
if (!isRoot.has(parent)) {
26+
isRoot.set(parent, true);
27+
}
28+
isRoot.set(child, false);
29+
map.set(parent, [left, right]);
30+
}
31+
const dfs = (val: number) => {
32+
if (val === 0) {
33+
return null;
34+
}
35+
const [left, right] = map.get(val) ?? [0, 0];
36+
return new TreeNode(val, dfs(left), dfs(right));
37+
};
38+
for (const [key, val] of isRoot.entries()) {
39+
if (val) {
40+
return dfs(key);
41+
}
42+
}
43+
return null;
44+
}

0 commit comments

Comments
 (0)