-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate_binary_tree_from_descriptions.java
More file actions
36 lines (31 loc) · 1.16 KB
/
Copy pathCreate_binary_tree_from_descriptions.java
File metadata and controls
36 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Daily_Problems;
import java.util.HashMap;
import java.util.HashSet;
import javax.swing.tree.TreeNode;
public class Create_binary_tree_from_descriptions {
public TreeNode createBinaryTree(int[][] descriptions) {
HashSet<Integer>ans=new HashSet<>();
HashMap<Integer,TreeNode> mp=new HashMap<>();
for(int i=0;i<descriptions.length;i++){
if(!mp.containsKey(descriptions[i][0])){
mp.put(descriptions[i][0],new TreeNode(descriptions[i][0]));
}
if(!mp.containsKey(descriptions[i][1])){
mp.put(descriptions[i][1],new TreeNode(descriptions[i][1]));
}
if(descriptions[i][2]==1){
mp.get(descriptions[i][0]).left=mp.get(descriptions[i][1]);
}
if(descriptions[i][2]==0){
mp.get(descriptions[i][0]).right=mp.get(descriptions[i][1]);
}
ans.add(descriptions[i][1]);
}
for(int i=0;i<descriptions.length;i++){
if(!ans.contains(descriptions[i][0])){
return mp.get(descriptions[i][0]);
}
}
return null;
}
}