forked from Red-0111/Anything-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeQuestion.java
58 lines (47 loc) · 1.49 KB
/
treeQuestion.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.HashMap;
public class treeQuestion{
public static void main(String[] args) {
}
private static int solve(TreeNode root){
HashMap<Integer,Integer> map = new HashMap<>();
int mul=1;
helper(root, 1, map);
for(int level:map.keySet()){
int num = map.get(level);
if(num==0) continue;
else mul=mul*num;
}
return mul;
}
private static int solve2(TreeNode root){
HashMap<Integer,Integer> map = new HashMap<>();
int max=Integer.MIN_VALUE;
helper2(root, 1, map);
for(int level:map.keySet()){
int num = map.get(level);
max = Math.min(max, num);
}
return max;
}
private static void helper2(TreeNode root,int level,HashMap<Integer,Integer> map){
if(root==null) return;
helper2(root.left, level+1,map);
helper2(root.right, level+1,map);
int prev = map.getOrDefault(level, Integer.MAX_VALUE);
if(prev>root.val) map.put(level,root.val);
}
private static void helper(TreeNode root,int level,HashMap<Integer,Integer> map){
if(root==null) return;
helper(root.left, level+1,map);
helper(root.right, level+1,map);
if(root.left==null || root.right==null) map.put(level,map.getOrDefault(level, 0)+root.val);
}
}
class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val){
this.val=val;
}
}