Skip to content

Commit 678ad90

Browse files
authored
Create 2196. Create Binary Tree From Descriptions (#530)
2 parents 6c254b1 + dda9b6d commit 678ad90

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
4+
5+
unordered_map<int, TreeNode*> mp;
6+
unordered_set<int> st;
7+
8+
for(auto desc: descriptions){
9+
10+
int p = desc[0];
11+
int c = desc[1];
12+
int left = desc[2];
13+
14+
if(mp.find(p) == mp.end()){
15+
mp[p] = new TreeNode(p);
16+
}
17+
18+
if(mp.find(c) == mp.end()){
19+
mp[c] = new TreeNode(c);
20+
}
21+
22+
if(left == 1){
23+
mp[p]->left = mp[c];
24+
}else{
25+
mp[p]->right = mp[c];
26+
}
27+
28+
st.insert(c);
29+
30+
}
31+
32+
for(auto desc: descriptions){
33+
34+
int p = desc[0];
35+
36+
if(st.find(p) == st.end()){
37+
return mp[p];
38+
}
39+
40+
}
41+
42+
return nullptr;
43+
44+
}
45+
};

0 commit comments

Comments
 (0)