Skip to content

Commit 0aa667f

Browse files
committed
fd
1 parent aac7430 commit 0aa667f

File tree

9 files changed

+42
-32
lines changed

9 files changed

+42
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@
371371
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)| [Java](leetcode/solution/src/BeautifulArrangement.java)|80||
372372
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)| [Java](leetcode/solution/src/EncodeAndDecodeTinyURL.java)|100||
373373
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Java](leetcode/solution/src/ReverseStringII.java)|100||
374+
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Java](leetcode/solution/src/DiameterOfBinaryTree.java)|80|这题易错|
374375
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
375376
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
376377
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Java](leetcode/solution/src/MergeTwoBinaryTrees.java)|100|很简单|
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class DiameterOfBinaryTree {
2+
3+
/**
4+
* 这题和 124. Binary Tree Maximum Path Sum比较像
5+
* 都是分两种情况,一种是必须包括root的,带上必须包括left和right的单边结果;
6+
* 还有不是必须包括root的,对比left和right的双边结果
7+
* flag为true表示必须包括root,false表示不是必须包括root
8+
*/
9+
public int diameterOfBinaryTree(TreeNode root) {
10+
if (root == null) {
11+
return 0;
12+
}
13+
return dfs(root, false);
14+
}
15+
16+
public int dfs(TreeNode root, boolean flag) {
17+
if (root == null) {
18+
return -1;
19+
}
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));
23+
}
24+
}

leetcode/src/Main.java

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,29 @@
44

55
public class Main {
66

7-
public List<Integer> rightSideView(TreeNode root) {
8-
List<Integer> result = new LinkedList<>();
9-
7+
public static int diameterOfBinaryTree(TreeNode root) {
108
if (root == null) {
11-
return result;
9+
return 0;
1210
}
11+
return dfs(root, false);
12+
}
1313

14-
Queue<TreeNode> queue = new LinkedList<>();
15-
Queue<TreeNode> next = new LinkedList<>();
16-
17-
TreeNode last = null;
18-
19-
queue.offer(root);
20-
21-
while (!queue.isEmpty()) {
22-
TreeNode node = queue.poll();
23-
24-
last = node;
25-
26-
if (node.left != null) {
27-
next.offer(node.left);
28-
}
29-
30-
if (node.right != null) {
31-
next.offer(node.right);
32-
}
33-
34-
if (queue.isEmpty()) {
35-
queue.addAll(next);
36-
next.clear();
37-
result.add(last.val);
38-
}
14+
public static int dfs(TreeNode root, boolean flag) {
15+
if (root == null) {
16+
return -1;
3917
}
40-
41-
return result;
18+
return flag ? Math.max(dfs(root.left, true) + 1, dfs(root.right, true) + 1) :
19+
Math.max(Math.max(dfs(root.left, false), dfs(root.right, false)),
20+
dfs(root.left, true) + 2 + dfs(root.right, true));
4221
}
4322

4423
public static void main(String[] args) {
45-
24+
TreeNode node4 = new TreeNode(4);
25+
TreeNode node5 = new TreeNode(5);
26+
TreeNode node2 = new TreeNode(2, node4, node5);
27+
TreeNode node3 = new TreeNode(3);
28+
TreeNode node1 = new TreeNode(1, node2, node3);
29+
int len = diameterOfBinaryTree(node1);
30+
System.out.println(String.format("len = %d", len));
4631
}
4732
}

0 commit comments

Comments
 (0)