mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
Merge pull request #159 from brenobaptista/solution-543
Added solution 543
This commit is contained in:
@ -0,0 +1,47 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/halfrost/LeetCode-Go/structures"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TreeNode define
|
||||||
|
type TreeNode = structures.TreeNode
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* type TreeNode struct {
|
||||||
|
* Val int
|
||||||
|
* Left *TreeNode
|
||||||
|
* Right *TreeNode
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
|
||||||
|
func diameterOfBinaryTree(root *TreeNode) int {
|
||||||
|
result := 0
|
||||||
|
|
||||||
|
checkDiameter(root, &result)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkDiameter(root *TreeNode, result *int) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
left := checkDiameter(root.Left, result)
|
||||||
|
|
||||||
|
right := checkDiameter(root.Right, result)
|
||||||
|
|
||||||
|
*result = max(*result, left+right)
|
||||||
|
|
||||||
|
return max(left, right) + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(a int, b int) int {
|
||||||
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/halfrost/LeetCode-Go/structures"
|
||||||
|
)
|
||||||
|
|
||||||
|
type question543 struct {
|
||||||
|
para543
|
||||||
|
ans543
|
||||||
|
}
|
||||||
|
|
||||||
|
// para 是参数
|
||||||
|
// one 代表第一个参数
|
||||||
|
type para543 struct {
|
||||||
|
one []int
|
||||||
|
}
|
||||||
|
|
||||||
|
// ans 是答案
|
||||||
|
// one 代表第一个答案
|
||||||
|
type ans543 struct {
|
||||||
|
one int
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Problem543(t *testing.T) {
|
||||||
|
|
||||||
|
qs := []question543{
|
||||||
|
|
||||||
|
{
|
||||||
|
para543{[]int{1, 2, 3, 4, 5}},
|
||||||
|
ans543{3},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
para543{[]int{1, 2}},
|
||||||
|
ans543{1},
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
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}},
|
||||||
|
ans543{8},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("------------------------Leetcode Problem 543------------------------\n")
|
||||||
|
|
||||||
|
for _, q := range qs {
|
||||||
|
_, p := q.ans543, q.para543
|
||||||
|
fmt.Printf("【input】:%v ", p)
|
||||||
|
root := structures.Ints2TreeNode(p.one)
|
||||||
|
fmt.Printf("【output】:%v \n", diameterOfBinaryTree(root))
|
||||||
|
}
|
||||||
|
fmt.Printf("\n\n\n")
|
||||||
|
}
|
31
leetcode/0543.Diameter-of-Binary-Tree/README.md
Normal file
31
leetcode/0543.Diameter-of-Binary-Tree/README.md
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# [543. Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)
|
||||||
|
|
||||||
|
## 题目
|
||||||
|
|
||||||
|
Given the `root` of a binary tree, return 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`.
|
||||||
|
|
||||||
|
The **length** of a path between two nodes is represented by the number of edges between them.
|
||||||
|
|
||||||
|
**Example 1:**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
```
|
||||||
|
Input: root = [1,2,3,4,5]
|
||||||
|
Output: 3
|
||||||
|
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example 2:**
|
||||||
|
|
||||||
|
```
|
||||||
|
Input: root = [1,2]
|
||||||
|
Output: 1
|
||||||
|
```
|
||||||
|
|
||||||
|
**Constraints:**
|
||||||
|
|
||||||
|
- The number of nodes in the tree is in the range `[1, 104]`.
|
||||||
|
- `-100 <= Node.val <= 100`
|
Reference in New Issue
Block a user