Skip to content

Commit dd26b1d

Browse files
committed
Added solution 543
1 parent 578c85e commit dd26b1d

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-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+
}

0 commit comments

Comments
 (0)