Skip to content

Commit 040ac16

Browse files
committed
Update solution 0543
1 parent 1c662f4 commit 040ac16

25 files changed

+3213
-3014
lines changed

README.md

Lines changed: 1704 additions & 1665 deletions
Large diffs are not rendered by default.

leetcode/0543.Diameter-of-Binary-Tree/543. Diameter of Binary Tree.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,23 @@ type TreeNode = structures.TreeNode
1818

1919
func diameterOfBinaryTree(root *TreeNode) int {
2020
result := 0
21-
2221
checkDiameter(root, &result)
23-
2422
return result
2523
}
2624

2725
func checkDiameter(root *TreeNode, result *int) int {
2826
if root == nil {
2927
return 0
3028
}
31-
3229
left := checkDiameter(root.Left, result)
33-
3430
right := checkDiameter(root.Right, result)
35-
3631
*result = max(*result, left+right)
37-
3832
return max(left, right) + 1
3933
}
4034

4135
func max(a int, b int) int {
4236
if a > b {
4337
return a
4438
}
45-
4639
return b
4740
}
Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)
22

3+
34
## 题目
45

5-
Given the `root` of a binary tree, return the length of the **diameter** of the tree.
6+
Given the `root` of a binary tree, return *the length of the **diameter** of the tree*.
67

7-
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`.
8+
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`.
89

9-
The **length** of a path between two nodes is represented by the number of edges between them.
10+
The **length** of a path between two nodes is represented by the number of edges between them.
1011

1112
**Example 1:**
1213

@@ -16,16 +17,71 @@ The **length** of a path between two nodes is represented by the number of edges
1617
Input: root = [1,2,3,4,5]
1718
Output: 3
1819
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
20+
1921
```
2022

2123
**Example 2:**
2224

2325
```
2426
Input: root = [1,2]
2527
Output: 1
28+
2629
```
2730

2831
**Constraints:**
2932

30-
- The number of nodes in the tree is in the range `[1, 104]`.
31-
- `-100 <= Node.val <= 100`
33+
- The number of nodes in the tree is in the range `[1, 104]`.
34+
- `100 <= Node.val <= 100`
35+
36+
## 题目大意
37+
38+
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
39+
40+
## 解题思路
41+
42+
- 简单题。遍历每个节点的左子树和右子树,累加从左子树到右子树的最大长度。遍历每个节点时,动态更新这个最大长度即可。
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
import (
50+
"github.com/halfrost/LeetCode-Go/structures"
51+
)
52+
53+
// TreeNode define
54+
type TreeNode = structures.TreeNode
55+
56+
/**
57+
* Definition for a binary tree node.
58+
* type TreeNode struct {
59+
* Val int
60+
* Left *TreeNode
61+
* Right *TreeNode
62+
* }
63+
*/
64+
65+
func diameterOfBinaryTree(root *TreeNode) int {
66+
result := 0
67+
checkDiameter(root, &result)
68+
return result
69+
}
70+
71+
func checkDiameter(root *TreeNode, result *int) int {
72+
if root == nil {
73+
return 0
74+
}
75+
left := checkDiameter(root.Left, result)
76+
right := checkDiameter(root.Right, result)
77+
*result = max(*result, left+right)
78+
return max(left, right) + 1
79+
}
80+
81+
func max(a int, b int) int {
82+
if a > b {
83+
return a
84+
}
85+
return b
86+
}
87+
```

website/content/ChapterFour/0500~0599/0542.01-Matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,5 @@ func updateMatrixDP(matrix [][]int) [][]int {
205205
----------------------------------------------
206206
<div style="display: flex;justify-content: space-between;align-items: center;">
207207
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0541.Reverse-String-II/">⬅️上一页</a></p>
208-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0547.Number-of-Provinces/">下一页➡️</a></p>
208+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0543.Diameter-of-Binary-Tree/">下一页➡️</a></p>
209209
</div>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)
2+
3+
4+
## 题目
5+
6+
Given the `root` of a binary tree, return *the length of the **diameter** of the tree*.
7+
8+
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`.
9+
10+
The **length** of a path between two nodes is represented by the number of edges between them.
11+
12+
**Example 1:**
13+
14+
![https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg](https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg)
15+
16+
```
17+
Input: root = [1,2,3,4,5]
18+
Output: 3
19+
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
20+
21+
```
22+
23+
**Example 2:**
24+
25+
```
26+
Input: root = [1,2]
27+
Output: 1
28+
29+
```
30+
31+
**Constraints:**
32+
33+
- The number of nodes in the tree is in the range `[1, 104]`.
34+
- `100 <= Node.val <= 100`
35+
36+
## 题目大意
37+
38+
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。
39+
40+
## 解题思路
41+
42+
- 简单题。遍历每个节点的左子树和右子树,累加从左子树到右子树的最大长度。遍历每个节点时,动态更新这个最大长度即可。
43+
44+
## 代码
45+
46+
```go
47+
package leetcode
48+
49+
import (
50+
"github.com/halfrost/LeetCode-Go/structures"
51+
)
52+
53+
// TreeNode define
54+
type TreeNode = structures.TreeNode
55+
56+
/**
57+
* Definition for a binary tree node.
58+
* type TreeNode struct {
59+
* Val int
60+
* Left *TreeNode
61+
* Right *TreeNode
62+
* }
63+
*/
64+
65+
func diameterOfBinaryTree(root *TreeNode) int {
66+
result := 0
67+
checkDiameter(root, &result)
68+
return result
69+
}
70+
71+
func checkDiameter(root *TreeNode, result *int) int {
72+
if root == nil {
73+
return 0
74+
}
75+
left := checkDiameter(root.Left, result)
76+
right := checkDiameter(root.Right, result)
77+
*result = max(*result, left+right)
78+
return max(left, right) + 1
79+
}
80+
81+
func max(a int, b int) int {
82+
if a > b {
83+
return a
84+
}
85+
return b
86+
}
87+
```
88+
89+
90+
----------------------------------------------
91+
<div style="display: flex;justify-content: space-between;align-items: center;">
92+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0542.01-Matrix/">⬅️上一页</a></p>
93+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0547.Number-of-Provinces/">下一页➡️</a></p>
94+
</div>

website/content/ChapterFour/0500~0599/0547.Number-of-Provinces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ func dfs547(M [][]int, cur int, visited []bool) {
113113

114114
----------------------------------------------
115115
<div style="display: flex;justify-content: space-between;align-items: center;">
116-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0542.01-Matrix/">⬅️上一页</a></p>
116+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0543.Diameter-of-Binary-Tree/">⬅️上一页</a></p>
117117
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0554.Brick-Wall/">下一页➡️</a></p>
118118
</div>

0 commit comments

Comments
 (0)