-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
1080.java
151 lines (138 loc) · 4.41 KB
/
1080.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
__________________________________________________________________________________________________
sample 1 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
class Pair {
TreeNode node;
int max_val;
Pair(TreeNode n, int v) {
node = n;
max_val = v;
}
}
public TreeNode sufficientSubset(TreeNode root, int limit) {
Pair res = helper(root, 0, limit);
return res.node;
}
Pair helper(TreeNode root, int cur, int limit) {
Pair left = null;
Pair right = null;
if (root.left == null && root.right == null) {
if (cur + root.val < limit) {
return new Pair(null, root.val);
} else {
return new Pair(root, root.val);
}
}
int max_val = Integer.MIN_VALUE;
if (root.left != null) {
left = helper(root.left, cur+root.val, limit);
max_val = Math.max(left.max_val, max_val);
}
if (root.right != null) {
right = helper(root.right, cur+root.val, limit);
max_val = Math.max(right.max_val, max_val);
}
if (left != null && left.node == null) {
root.left = null;
}
if (right != null && right.node == null) {
root.right = null;
}
// if (root.val == 11) {
// System.out.println("mv=" + max_val + ", left:"
// + left.node + ", " +left.max_val
// + ", right:" + right.node + ", " + right.max_val);
// }
if (max_val + root.val + cur < limit) {
return new Pair(null, max_val + root.val);
} else {
return new Pair(root, max_val + root.val);
}
}
}
__________________________________________________________________________________________________
sample 2 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sufficientSubset(TreeNode root, int limit) {
TreeNode dummy = new TreeNode(0);
dummy.left = root;
check(dummy, 0, limit);
return dummy.left;
}
private void check(TreeNode node, int sumFromRoot, int limit){
if (node.left != null){
if (sumFromRoot + node.val + getMaxSum(node.left) < limit){
node.left = null;
} else {
check(node.left, sumFromRoot + node.val, limit);
}
}
if (node.right != null){
if (sumFromRoot + node.val + getMaxSum(node.right) < limit){
node.right = null;
} else {
check(node.right, sumFromRoot + node.val, limit);
}
}
}
private int getMaxSum(TreeNode node){
if (node.left == null && node.right == null){
return node.val;
} else if (node.left == null){
return node.val + getMaxSum(node.right);
} else if (node.right == null){
return node.val + getMaxSum(node.left);
} else {
return node.val + Math.max(getMaxSum(node.right), getMaxSum(node.left));
}
}
}
__________________________________________________________________________________________________
sample 3 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sufficientSubset(TreeNode root, int limit) {
if(!isValid(root,limit)) return null;
delete(root,limit);
return root;
}
private void delete(TreeNode node,int limit){
if(node==null) return;
if(isValid(node.left,limit-node.val)) delete(node.left,limit-node.val);
else node.left=null;
if(isValid(node.right,limit-node.val)) delete(node.right,limit-node.val);
else node.right=null;
}
private boolean isValid(TreeNode node,int limit){
if(node==null) return false;
if(node.left==null&&node.right==null) return node.val>=limit;
boolean left=isValid(node.left,limit-node.val),right=isValid(node.right,limit-node.val);
return left||right;
}
}