Skip to content

Commit ea2fa7c

Browse files
committed
DiameterOfBinaryTree543
1 parent 431b0d6 commit ea2fa7c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232

3333

3434

35-
# Total: 166
35+
# Total: 167
3636

3737
| Easy | Medium | Hard |
3838
|:----:|:------:|:----:|
39-
| 36 | 98 | 32 |
39+
| 37 | 98 | 32 |
4040

4141

4242
| Question | Solution | Difficulty |
@@ -192,6 +192,7 @@
192192
| [513. Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindBottomLeftTreeValue513.java) | Medium |
193193
| [525. Contiguous Array](https://leetcode.com/problems/contiguous-array/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ContiguousArray525.java) | Medium |
194194
| [535. Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/EncodeAndDecodeTinyURL535.java) | Medium |
195+
| [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/DiameterOfBinaryTree543.java) | Easy |
195196
| [547. Friend Circles](https://leetcode.com/problems/friend-circles/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FriendCircles547.java) | Medium |
196197
| [554. Brick Wall](https://leetcode.com/problems/brick-wall/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/BrickWall554.java) | Medium |
197198
| [560. Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SubarraySumEqualsK560.java) | Medium |

Diff for: src/DiameterOfBinaryTree543.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Given a binary tree, you need to compute the length of the diameter of the
3+
* tree. The diameter of a binary tree is the length of the longest pathbetween
4+
* any two nodes in a tree. This path may or may not pass through the root.
5+
*
6+
* Example:
7+
* Given a binary tree
8+
* 1
9+
* / \
10+
* 2 3
11+
* / \
12+
* 4 5
13+
* Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
14+
*
15+
* Note: The length of path between two nodes is represented by the number of
16+
* edges between them.
17+
*
18+
*/
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* int val;
24+
* TreeNode left;
25+
* TreeNode right;
26+
* TreeNode(int x) { val = x; }
27+
* }
28+
*/
29+
30+
public class DiameterOfBinaryTree543 {
31+
public int diameterOfBinaryTree(TreeNode root) {
32+
return helper(root)[0];
33+
}
34+
35+
private int[] helper(TreeNode root) {
36+
if (root == null) return new int[]{0, 0};
37+
38+
int[] l = helper(root.left);
39+
int[] r = helper(root.right);
40+
41+
return new int[]{Math.max(Math.max(l[0], r[0]), l[1] + r[1]), Math.max(l[1], r[1]) + 1};
42+
}
43+
44+
}

0 commit comments

Comments
 (0)