We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6c254b1 + dda9b6d commit 678ad90Copy full SHA for 678ad90
2196. Create Binary Tree From Descriptions
@@ -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
33
34
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