mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 01:15:57 +08:00
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
|
||||
}
|
Reference in New Issue
Block a user