Skip to content

Commit 1d51c1c

Browse files
author
liwentian
committed
fd
1 parent a7ba2d0 commit 1d51c1c

File tree

5 files changed

+82
-36
lines changed

5 files changed

+82
-36
lines changed
Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.leetcode.amazon;
22

3+
import com.leetcode.library.TreeNode;
4+
35
import java.util.HashMap;
46
import java.util.LinkedHashMap;
57

@@ -9,13 +11,43 @@
911

1012
public class Main {
1113

12-
public static void main(String[] args) {
13-
HashMap<String, String> map = new HashMap<>();
14-
map.put("one", "ones");
15-
map.put("two", "twos");
14+
public class LargestBSTSubtree {
15+
16+
class Result {
17+
int count;
18+
int lower;
19+
int upper;
20+
21+
Result(int count, int lower, int upper) {
22+
this.count = count;
23+
this.lower = lower;
24+
this.upper = upper;
25+
}
26+
}
27+
28+
public int largestBSTSubtree(TreeNode root) {
29+
Result res = helper(root);
30+
return Math.abs(res.count);
31+
}
1632

17-
for (Integer key : map.keySet()) {
18-
System.out.println(key);
33+
private Result helper(TreeNode root) {
34+
if (root == null) {
35+
return new Result(0, Integer.MAX_VALUE, Integer.MIN_VALUE);
36+
}
37+
38+
Result left = helper(root.left);
39+
Result right = helper(root.right);
40+
41+
// 注意这里的等号千万别掉了,因为可能树中有节点相同
42+
if (left.count < 0 || right.count < 0 || left.upper >= root.val || right.lower <= root.val) {
43+
return new Result(-1 * Math.max(Math.abs(left.count), Math.abs(right.count)), 0, 0);
44+
}
45+
46+
return new Result(left.count + right.count + 1, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
1947
}
2048
}
49+
50+
public static void main(String[] args) {
51+
52+
}
2153
}

ebook/array/Array.tex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,21 @@ \subsubsection{Solution}
161161

162162
\begin{Code}
163163
public boolean findTarget(TreeNode root, int k) {
164-
HashSet<Integer> set = new HashSet<>();
165-
return dfs(root, set, k);
164+
List<Integer> nums = new ArrayList<>();
165+
inorder(root, nums);
166+
for (int i = 0, j = nums.size() - 1; i < j; ) {
167+
if (nums.get(i) + nums.get(j) == k) return true;
168+
if (nums.get(i) + nums.get(j) < k) i++;
169+
else j--;
170+
}
171+
return false;
166172
}
167173

168-
private boolean dfs(TreeNode node, HashSet<Integer> set, int k) {
169-
if (node == null) {
170-
return false;
171-
}
172-
if (set.contains(k - node.val)) {
173-
return true;
174-
}
175-
set.add(node.val);
176-
return dfs(node.left, set, k) || dfs(node.right, set, k);
174+
public void inorder(TreeNode root, List<Integer> nums) {
175+
if (root == null) return;
176+
inorder(root.left, nums);
177+
nums.add(root.val);
178+
inorder(root.right, nums);
177179
}
178180
\end{Code}
179181

ebook/tree/Tree.tex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,9 +2734,11 @@ \subsubsection{Solution}
27342734
}
27352735
}
27362736

2737+
int max = 0;
2738+
27372739
public int largestBSTSubtree(TreeNode root) {
2738-
Result res = helper(root);
2739-
return Math.abs(res.count);
2740+
helper(root);
2741+
return max;
27402742
}
27412743

27422744
private Result helper(TreeNode root) {
@@ -2749,10 +2751,12 @@ \subsubsection{Solution}
27492751

27502752
// 注意这里的等号千万别掉了,因为可能树中有节点相同
27512753
if (left.count < 0 || right.count < 0 || left.upper >= root.val || right.lower <= root.val) {
2752-
return new Result(-1 * Math.max(Math.abs(left.count), Math.abs(right.count)), 0, 0);
2754+
return new Result(-1, 0, 0);
27532755
}
27542756

2755-
return new Result(left.count + right.count + 1, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
2757+
int size = left.count + 1 + right.count;
2758+
max = Math.max(size, max);
2759+
return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
27562760
}
27572761
\end{Code}
27582762

solution/src/main/java/com/inuker/solution/LargestBSTSubtree.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class Result {
2020
}
2121
}
2222

23+
int max = 0;
24+
2325
public int largestBSTSubtree(TreeNode root) {
24-
Result res = helper(root);
25-
return Math.abs(res.count);
26+
helper(root);
27+
return max;
2628
}
2729

2830
private Result helper(TreeNode root) {
@@ -35,9 +37,11 @@ private Result helper(TreeNode root) {
3537

3638
// 注意这里的等号千万别掉了,因为可能树中有节点相同
3739
if (left.count < 0 || right.count < 0 || left.upper >= root.val || right.lower <= root.val) {
38-
return new Result(-1 * Math.max(Math.abs(left.count), Math.abs(right.count)), 0, 0);
40+
return new Result(-1, 0, 0);
3941
}
4042

41-
return new Result(left.count + right.count + 1, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
43+
int size = left.count + 1 + right.count;
44+
max = Math.max(size, max);
45+
return new Result(size, Math.min(left.lower, root.val), Math.max(right.upper, root.val));
4246
}
4347
}

solution/src/main/java/com/inuker/solution/TwoSumIV.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.leetcode.library.TreeNode;
44

5+
import java.util.ArrayList;
56
import java.util.HashSet;
7+
import java.util.List;
68

79
/**
810
* Created by liwentian on 2017/9/10.
@@ -14,18 +16,20 @@
1416
public class TwoSumIV {
1517

1618
public boolean findTarget(TreeNode root, int k) {
17-
HashSet<Integer> set = new HashSet<>();
18-
return dfs(root, set, k);
19+
List<Integer> nums = new ArrayList<>();
20+
inorder(root, nums);
21+
for (int i = 0, j = nums.size() - 1; i < j; ) {
22+
if (nums.get(i) + nums.get(j) == k) return true;
23+
if (nums.get(i) + nums.get(j) < k) i++;
24+
else j--;
25+
}
26+
return false;
1927
}
2028

21-
private boolean dfs(TreeNode node, HashSet<Integer> set, int k) {
22-
if (node == null) {
23-
return false;
24-
}
25-
if (set.contains(k - node.val)) {
26-
return true;
27-
}
28-
set.add(node.val);
29-
return dfs(node.left, set, k) || dfs(node.right, set, k);
29+
public void inorder(TreeNode root, List<Integer> nums) {
30+
if (root == null) return;
31+
inorder(root.left, nums);
32+
nums.add(root.val);
33+
inorder(root.right, nums);
3034
}
3135
}

0 commit comments

Comments
 (0)