Skip to content

Commit 4de91c1

Browse files
committed
feat: add 543
1 parent 5aaebae commit 4de91c1

File tree

7 files changed

+153
-154
lines changed

7 files changed

+153
-154
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
|119|[Pascal's Triangle II][119]|Array|
5151
|121|[Best Time to Buy and Sell Stock][121]|Array, Dynamic Programmin|
5252
|122|[Best Time to Buy and Sell Stock II][122]|Array, Greedy|
53+
|543|[Diameter of Binary Tree][543]|Tree|
5354

5455

5556
## Medium
@@ -108,10 +109,12 @@
108109
[119]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/119/README.md
109110
[121]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/121/README.md
110111
[122]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/122/README.md
112+
[543]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/543/README.md
111113

112114
[002]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/002/README.md
113115
[003]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/003/README.md
114116
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
115117
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
118+
[554]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/554/README.md
116119

117120
[004]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/004/README.md

note/543/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [Diameter of Binary Tree][title]
2+
3+
## Description
4+
5+
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the **longest** path between any two nodes in a tree. This path may or may not pass through the root.
6+
7+
**Example:**
8+
Given a binary tree
9+
10+
```
11+
1
12+
/ \
13+
2 3
14+
/ \
15+
4 5
16+
17+
```
18+
19+
Return **3**, which is the length of the path [4,2,1,3] or [5,2,1,3].
20+
21+
**Note:** The length of path between two nodes is represented by the number of edges between them.
22+
23+
**Tags:** Tree
24+
25+
26+
## 思路
27+
28+
题意是让你算出二叉树中最远的两个节点的距离,分别计算左右子树的最大高度,然后不断迭代出其和的最大值就是最终结果。
29+
30+
``` java
31+
/**
32+
* Definition for a binary tree node.
33+
* public class TreeNode {
34+
* int val;
35+
* TreeNode left;
36+
* TreeNode right;
37+
* TreeNode(int x) { val = x; }
38+
* }
39+
*/
40+
class Solution {
41+
int max = 0;
42+
43+
public int diameterOfBinaryTree(TreeNode root) {
44+
helper(root);
45+
return max;
46+
}
47+
48+
private int helper(TreeNode root) {
49+
if (root == null) return 0;
50+
int l = helper(root.left);
51+
int r = helper(root.right);
52+
if (l + r > max) max = l + r;
53+
return Math.max(l, r) + 1;
54+
}
55+
}
56+
```
57+
58+
59+
## 结语
60+
61+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
62+
63+
64+
65+
[title]: https://leetcode.com/problems/diameter-of-binary-tree
66+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+50-154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.blankj.easy._543;
2+
3+
4+
import com.blankj.structure.TreeNode;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/10/13
11+
* desc :
12+
* </pre>
13+
*/
14+
public class Solution {
15+
int max = 0;
16+
17+
public int diameterOfBinaryTree(TreeNode root) {
18+
helper(root);
19+
return max;
20+
}
21+
22+
private int helper(TreeNode root) {
23+
if (root == null) return 0;
24+
int l = helper(root.left);
25+
int r = helper(root.right);
26+
if (l + r > max) max = l + r;
27+
return Math.max(l, r) + 1;
28+
}
29+
30+
public static void main(String[] args) {
31+
Solution solution = new Solution();
32+
System.out.println(solution.diameterOfBinaryTree(TreeNode.createTestData("[1,2,3,4,5]")));
33+
}
34+
}

0 commit comments

Comments
 (0)