-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfGoodLeafNodePairs.java
122 lines (120 loc) · 3.6 KB
/
NumberOfGoodLeafNodePairs.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int result;
int distance;
public int countPairs(TreeNode root, int distance) {
result = 0;
this.distance = distance;
solve(root);
return result;
}
private HashMap<Integer,Integer> solve(TreeNode root)
{
if (root.left == null && root.right == null)
{
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(0,1);
return map;
}
HashMap<Integer,Integer> left = new HashMap<Integer,Integer>(), right = new HashMap<Integer,Integer>();
if (root.left != null) left = solve(root.left);
if (root.right != null) right = solve(root.right);
for (Map.Entry elem1 : left.entrySet())
{
for (Map.Entry elem2 : right.entrySet())
{
if ((Integer)elem1.getKey()+(Integer)elem2.getKey()+2 <= distance)
{
result += (Integer)elem1.getValue()*(Integer)elem2.getValue();
}
}
}
HashMap<Integer,Integer> newMap = new HashMap<Integer,Integer>();
int key, value;
for (Map.Entry elem : left.entrySet())
{
key = (Integer)elem.getKey();
value = (Integer)elem.getValue();
newMap.put(key+1,newMap.getOrDefault(key+1,0)+value);
}
for (Map.Entry elem : right.entrySet())
{
key = (Integer)elem.getKey();
value = (Integer)elem.getValue();
newMap.put(key+1,newMap.getOrDefault(key+1,0)+value);
}
return newMap;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int result;
int distance;
public int countPairs(TreeNode root, int distance) {
result = 0;
this.distance = distance;
solve(root);
return result;
}
private int[] solve(TreeNode root)
{
if (root.left == null && root.right == null)
{
int[] map = new int[25];
Arrays.fill(map,-1);
map[0] = 1;
return map;
}
int[] left = new int[25], right = new int[25];
Arrays.fill(left,-1);
Arrays.fill(right,-1);
if (root.left != null) left = solve(root.left);
if (root.right != null) right = solve(root.right);
int i, j;
for (i = 0; i < 25; ++i)
{
if (i > distance) break;
if (left[i] == -1) continue;
for (j = 0; j < 25; ++j)
{
if (i+j+2 > distance) break;
if (right[j] == -1) continue;
result += left[i]*right[j];
}
}
int[] hash = new int[25];
Arrays.fill(hash,-1);
for (i = 0; i < 24; ++i)
hash[i+1] = left[i] == -1 ? (right[i] == -1 ? -1 : right[i]) : (right[i] == -1 ? left[i] : left[i]+right[i]);
return hash;
}
}