Skip to content

Commit 48fc7a0

Browse files
committed
udpate some solution
1 parent 99e8e95 commit 48fc7a0

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

leetcode/solution/src/DiameterOfBinaryTree.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@ public class DiameterOfBinaryTree {
22

33
/**
44
* 这题和 124. Binary Tree Maximum Path Sum比较像
5-
* 都是分两种情况,一种是必须包括root的,带上必须包括left和right的单边结果;
6-
* 还有不是必须包括root的,对比left和right的双边结果
7-
* flag为true表示必须包括root,false表示不是必须包括root
85
*/
96
public int diameterOfBinaryTree(TreeNode root) {
107
if (root == null) {
118
return 0;
129
}
13-
return dfs(root, false);
10+
return dfs(root, new int[1]) - 1;
1411
}
1512

16-
public int dfs(TreeNode root, boolean flag) {
13+
/**
14+
* len表示带上root的最大深度
15+
*/
16+
private int dfs(TreeNode root, int[] len) {
1717
if (root == null) {
18-
return -1;
18+
return 0;
1919
}
20-
return flag ? Math.max(dfs(root.left, true) + 1, dfs(root.right, true) + 1) :
21-
Math.max(Math.max(dfs(root.left, false), dfs(root.right, false)),
22-
dfs(root.left, true) + 2 + dfs(root.right, true));
20+
21+
int[] lt = new int[1];
22+
int[] rt = new int[1];
23+
24+
int left = dfs(root.left, lt);
25+
int right = dfs(root.right, rt);
26+
27+
len[0] = Math.max(lt[0], rt[0]) + 1;
28+
29+
return Math.max(Math.max(left, right), lt[0] + rt[0] + 1);
2330
}
2431
}

leetcode/solution/src/TwoSumIV.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import java.util.ArrayList;
2+
import java.util.HashSet;
23
import java.util.List;
34

45
/**
56
* https://leetcode.com/articles/two-sum-iv/
67
*/
78
public class TwoSumIV {
89

10+
/**
11+
* 先遍历一遍,生成一个递增序列,再从两端往中间检查
12+
*/
913
public boolean findTarget(TreeNode root, int k) {
1014
List<Integer> nums = new ArrayList<>();
1115
inorder(root, nums);
@@ -23,4 +27,23 @@ public void inorder(TreeNode root, List<Integer> nums) {
2327
nums.add(root.val);
2428
inorder(root.right, nums);
2529
}
30+
31+
/**
32+
* 更简单一点,遍历树,用一个hashset记录每个元素
33+
* 时间空间都是O(n)
34+
*/
35+
public boolean findTarget2(TreeNode root, int k) {
36+
return visit(root, k, new HashSet<>());
37+
}
38+
39+
private boolean visit(TreeNode root, int k, HashSet<Integer> set) {
40+
if (root == null) {
41+
return false;
42+
}
43+
if (set.contains(k - root.val)) {
44+
return true;
45+
}
46+
set.add(root.val);
47+
return visit(root.left, k, set) || visit(root.right, k, set);
48+
}
2649
}

leetcode/src/Main.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,33 @@ public class Main {
66

77
public static class Solution {
88

9-
public TreeNode constructMaximumBinaryTree(int[] nums) {
10-
return helper(nums, 0, nums.length - 1);
9+
public int diameterOfBinaryTree(TreeNode root) {
10+
if (root == null) {
11+
return 0;
12+
}
13+
return dfs(root, new int[1]) - 1;
1114
}
1215

13-
private TreeNode helper(int[] nums, int start, int end) {
14-
if (start > end) {
15-
return null;
16-
}
17-
int index, max = start;
18-
for (index = start + 1; index <= end; index++) {
19-
if (nums[index] > nums[max]) {
20-
max = index;
21-
}
16+
private int dfs(TreeNode root, int[] len) {
17+
if (root == null) {
18+
return 0;
2219
}
23-
TreeNode root = new TreeNode(nums[max]);
24-
root.left = helper(nums, start, max - 1);
25-
root.right = helper(nums, max + 1, end);
26-
return root;
20+
21+
int[] lt = new int[1];
22+
int[] rt = new int[1];
23+
24+
int left = dfs(root.left, lt);
25+
int right = dfs(root.right, rt);
26+
27+
len[0] = Math.max(lt[0], rt[0]) + 1;
28+
29+
return Math.max(Math.max(left, right), lt[0] + rt[0] + 1);
2730
}
2831
}
2932

3033
public static void main(String[] args) {
31-
34+
Solution s = new Solution();
35+
TreeNode root1 = new TreeNode(1);
36+
System.out.println(f);
3237
}
3338
}

0 commit comments

Comments
 (0)