Skip to content

Commit b6f76f1

Browse files
committed
feat: add solutions to lc problem: No.2196
No.2196.Create Binary Tree From Descriptions
1 parent 1d444d0 commit b6f76f1

File tree

6 files changed

+413
-4
lines changed

6 files changed

+413
-4
lines changed

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

+141-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,78 @@
6363
<!-- 这里可写当前语言的特殊实现逻辑 -->
6464

6565
```python
66-
66+
# Definition for a binary tree node.
67+
# class TreeNode:
68+
# def __init__(self, val=0, left=None, right=None):
69+
# self.val = val
70+
# self.left = left
71+
# self.right = right
72+
class Solution:
73+
def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
74+
g = defaultdict(TreeNode)
75+
vis = set()
76+
for p, c, left in descriptions:
77+
if p not in g:
78+
g[p] = TreeNode(p)
79+
if c not in g:
80+
g[c] = TreeNode(c)
81+
if left:
82+
g[p].left = g[c]
83+
else:
84+
g[p].right = g[c]
85+
vis.add(c)
86+
for v, node in g.items():
87+
if v not in vis:
88+
return node
6789
```
6890

6991
### **Java**
7092

7193
<!-- 这里可写当前语言的特殊实现逻辑 -->
7294

7395
```java
74-
96+
/**
97+
* Definition for a binary tree node.
98+
* public class TreeNode {
99+
* int val;
100+
* TreeNode left;
101+
* TreeNode right;
102+
* TreeNode() {}
103+
* TreeNode(int val) { this.val = val; }
104+
* TreeNode(int val, TreeNode left, TreeNode right) {
105+
* this.val = val;
106+
* this.left = left;
107+
* this.right = right;
108+
* }
109+
* }
110+
*/
111+
class Solution {
112+
public TreeNode createBinaryTree(int[][] descriptions) {
113+
Map<Integer, TreeNode> m = new HashMap<>();
114+
Set<Integer> vis = new HashSet<>();
115+
for (int[] d : descriptions) {
116+
int p = d[0], c = d[1], isLeft = d[2];
117+
if (!m.containsKey(p)) {
118+
m.put(p, new TreeNode(p));
119+
}
120+
if (!m.containsKey(c)) {
121+
m.put(c, new TreeNode(c));
122+
}
123+
if (isLeft == 1) {
124+
m.get(p).left = m.get(c);
125+
} else {
126+
m.get(p).right = m.get(c);
127+
}
128+
vis.add(c);
129+
}
130+
for (Map.Entry<Integer, TreeNode> entry : m.entrySet()) {
131+
if (!vis.contains(entry.getKey())) {
132+
return entry.getValue();
133+
}
134+
}
135+
return null;
136+
}
137+
}
75138
```
76139

77140
### **TypeScript**
@@ -188,6 +251,82 @@ impl Solution {
188251
}
189252
```
190253

254+
### **C++**
255+
256+
```cpp
257+
/**
258+
* Definition for a binary tree node.
259+
* struct TreeNode {
260+
* int val;
261+
* TreeNode *left;
262+
* TreeNode *right;
263+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
264+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
265+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
266+
* };
267+
*/
268+
class Solution {
269+
public:
270+
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
271+
unordered_map<int, TreeNode*> m;
272+
unordered_set<int> vis;
273+
for (auto& d : descriptions)
274+
{
275+
int p = d[0], c = d[1], left = d[2];
276+
if (!m.count(p)) m[p] = new TreeNode(p);
277+
if (!m.count(c)) m[c] = new TreeNode(c);
278+
if (left) m[p]->left = m[c];
279+
else m[p]->right = m[c];
280+
vis.insert(c);
281+
}
282+
for (auto& [v, node] : m)
283+
{
284+
if (!vis.count(v)) return node;
285+
}
286+
return nullptr;
287+
}
288+
};
289+
```
290+
291+
### **Go**
292+
293+
```go
294+
/**
295+
* Definition for a binary tree node.
296+
* type TreeNode struct {
297+
* Val int
298+
* Left *TreeNode
299+
* Right *TreeNode
300+
* }
301+
*/
302+
func createBinaryTree(descriptions [][]int) *TreeNode {
303+
m := make(map[int]*TreeNode)
304+
vis := make(map[int]bool)
305+
for _, d := range descriptions {
306+
p, c, left := d[0], d[1], d[2]
307+
if m[p] == nil {
308+
m[p] = &TreeNode{Val: p}
309+
}
310+
if m[c] == nil {
311+
m[c] = &TreeNode{Val: c}
312+
}
313+
if left == 1 {
314+
m[p].Left = m[c]
315+
} else {
316+
m[p].Right = m[c]
317+
}
318+
vis[c] = true
319+
}
320+
321+
for v, node := range m {
322+
if !vis[v] {
323+
return node
324+
}
325+
}
326+
return nil
327+
}
328+
```
329+
191330
### **...**
192331

193332
```

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

+141-2
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,76 @@ The resulting binary tree is shown in the diagram.
5252
### **Python3**
5353

5454
```python
55-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
61+
class Solution:
62+
def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
63+
g = defaultdict(TreeNode)
64+
vis = set()
65+
for p, c, left in descriptions:
66+
if p not in g:
67+
g[p] = TreeNode(p)
68+
if c not in g:
69+
g[c] = TreeNode(c)
70+
if left:
71+
g[p].left = g[c]
72+
else:
73+
g[p].right = g[c]
74+
vis.add(c)
75+
for v, node in g.items():
76+
if v not in vis:
77+
return node
5678
```
5779

5880
### **Java**
5981

6082
```java
61-
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode() {}
90+
* TreeNode(int val) { this.val = val; }
91+
* TreeNode(int val, TreeNode left, TreeNode right) {
92+
* this.val = val;
93+
* this.left = left;
94+
* this.right = right;
95+
* }
96+
* }
97+
*/
98+
class Solution {
99+
public TreeNode createBinaryTree(int[][] descriptions) {
100+
Map<Integer, TreeNode> m = new HashMap<>();
101+
Set<Integer> vis = new HashSet<>();
102+
for (int[] d : descriptions) {
103+
int p = d[0], c = d[1], isLeft = d[2];
104+
if (!m.containsKey(p)) {
105+
m.put(p, new TreeNode(p));
106+
}
107+
if (!m.containsKey(c)) {
108+
m.put(c, new TreeNode(c));
109+
}
110+
if (isLeft == 1) {
111+
m.get(p).left = m.get(c);
112+
} else {
113+
m.get(p).right = m.get(c);
114+
}
115+
vis.add(c);
116+
}
117+
for (Map.Entry<Integer, TreeNode> entry : m.entrySet()) {
118+
if (!vis.contains(entry.getKey())) {
119+
return entry.getValue();
120+
}
121+
}
122+
return null;
123+
}
124+
}
62125
```
63126

64127
### **TypeScript**
@@ -175,6 +238,82 @@ impl Solution {
175238
}
176239
```
177240

241+
### **C++**
242+
243+
```cpp
244+
/**
245+
* Definition for a binary tree node.
246+
* struct TreeNode {
247+
* int val;
248+
* TreeNode *left;
249+
* TreeNode *right;
250+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
251+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
252+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
253+
* };
254+
*/
255+
class Solution {
256+
public:
257+
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
258+
unordered_map<int, TreeNode*> m;
259+
unordered_set<int> vis;
260+
for (auto& d : descriptions)
261+
{
262+
int p = d[0], c = d[1], left = d[2];
263+
if (!m.count(p)) m[p] = new TreeNode(p);
264+
if (!m.count(c)) m[c] = new TreeNode(c);
265+
if (left) m[p]->left = m[c];
266+
else m[p]->right = m[c];
267+
vis.insert(c);
268+
}
269+
for (auto& [v, node] : m)
270+
{
271+
if (!vis.count(v)) return node;
272+
}
273+
return nullptr;
274+
}
275+
};
276+
```
277+
278+
### **Go**
279+
280+
```go
281+
/**
282+
* Definition for a binary tree node.
283+
* type TreeNode struct {
284+
* Val int
285+
* Left *TreeNode
286+
* Right *TreeNode
287+
* }
288+
*/
289+
func createBinaryTree(descriptions [][]int) *TreeNode {
290+
m := make(map[int]*TreeNode)
291+
vis := make(map[int]bool)
292+
for _, d := range descriptions {
293+
p, c, left := d[0], d[1], d[2]
294+
if m[p] == nil {
295+
m[p] = &TreeNode{Val: p}
296+
}
297+
if m[c] == nil {
298+
m[c] = &TreeNode{Val: c}
299+
}
300+
if left == 1 {
301+
m[p].Left = m[c]
302+
} else {
303+
m[p].Right = m[c]
304+
}
305+
vis[c] = true
306+
}
307+
308+
for v, node := range m {
309+
if !vis[v] {
310+
return node
311+
}
312+
}
313+
return nil
314+
}
315+
```
316+
178317
### **...**
179318

180319
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
15+
unordered_map<int, TreeNode*> m;
16+
unordered_set<int> vis;
17+
for (auto& d : descriptions)
18+
{
19+
int p = d[0], c = d[1], left = d[2];
20+
if (!m.count(p)) m[p] = new TreeNode(p);
21+
if (!m.count(c)) m[c] = new TreeNode(c);
22+
if (left) m[p]->left = m[c];
23+
else m[p]->right = m[c];
24+
vis.insert(c);
25+
}
26+
for (auto& [v, node] : m)
27+
{
28+
if (!vis.count(v)) return node;
29+
}
30+
return nullptr;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func createBinaryTree(descriptions [][]int) *TreeNode {
10+
m := make(map[int]*TreeNode)
11+
vis := make(map[int]bool)
12+
for _, d := range descriptions {
13+
p, c, left := d[0], d[1], d[2]
14+
if m[p] == nil {
15+
m[p] = &TreeNode{Val: p}
16+
}
17+
if m[c] == nil {
18+
m[c] = &TreeNode{Val: c}
19+
}
20+
if left == 1 {
21+
m[p].Left = m[c]
22+
} else {
23+
m[p].Right = m[c]
24+
}
25+
vis[c] = true
26+
}
27+
28+
for v, node := range m {
29+
if !vis[v] {
30+
return node
31+
}
32+
}
33+
return nil
34+
}

0 commit comments

Comments
 (0)