Skip to content

Commit 1c662f4

Browse files
authored
Merge pull request #159 from brenobaptista/solution-543
Added solution 543
2 parents 765bb77 + 8b41ad3 commit 1c662f4

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode
2+
3+
import (
4+
"github.com/halfrost/LeetCode-Go/structures"
5+
)
6+
7+
// TreeNode define
8+
type TreeNode = structures.TreeNode
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
func diameterOfBinaryTree(root *TreeNode) int {
20+
result := 0
21+
22+
checkDiameter(root, &result)
23+
24+
return result
25+
}
26+
27+
func checkDiameter(root *TreeNode, result *int) int {
28+
if root == nil {
29+
return 0
30+
}
31+
32+
left := checkDiameter(root.Left, result)
33+
34+
right := checkDiameter(root.Right, result)
35+
36+
*result = max(*result, left+right)
37+
38+
return max(left, right) + 1
39+
}
40+
41+
func max(a int, b int) int {
42+
if a > b {
43+
return a
44+
}
45+
46+
return b
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/halfrost/LeetCode-Go/structures"
8+
)
9+
10+
type question543 struct {
11+
para543
12+
ans543
13+
}
14+
15+
// para 是参数
16+
// one 代表第一个参数
17+
type para543 struct {
18+
one []int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans543 struct {
24+
one int
25+
}
26+
27+
func Test_Problem543(t *testing.T) {
28+
29+
qs := []question543{
30+
31+
{
32+
para543{[]int{1, 2, 3, 4, 5}},
33+
ans543{3},
34+
},
35+
36+
{
37+
para543{[]int{1, 2}},
38+
ans543{1},
39+
},
40+
41+
{
42+
para543{[]int{4, -7, -3, structures.NULL, structures.NULL, -9, -3, 9, -7, -4, structures.NULL, 6, structures.NULL, -6, -6, structures.NULL, structures.NULL, 0, 6, 5, structures.NULL, 9, structures.NULL, structures.NULL, -1, -4, structures.NULL, structures.NULL, structures.NULL, -2}},
43+
ans543{8},
44+
},
45+
}
46+
47+
fmt.Printf("------------------------Leetcode Problem 543------------------------\n")
48+
49+
for _, q := range qs {
50+
_, p := q.ans543, q.para543
51+
fmt.Printf("【input】:%v ", p)
52+
root := structures.Ints2TreeNode(p.one)
53+
fmt.Printf("【output】:%v \n", diameterOfBinaryTree(root))
54+
}
55+
fmt.Printf("\n\n\n")
56+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)
2+
3+
## 题目
4+
5+
Given the `root` of a binary tree, return the length of the **diameter** of the tree.
6+
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+
9+
The **length** of a path between two nodes is represented by the number of edges between them.
10+
11+
**Example 1:**
12+
13+
![https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg](https://assets.leetcode.com/uploads/2021/03/06/diamtree.jpg)
14+
15+
```
16+
Input: root = [1,2,3,4,5]
17+
Output: 3
18+
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: root = [1,2]
25+
Output: 1
26+
```
27+
28+
**Constraints:**
29+
30+
- The number of nodes in the tree is in the range `[1, 104]`.
31+
- `-100 <= Node.val <= 100`

0 commit comments

Comments
 (0)