File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
leetcode/0543.Diameter-of-Binary-Tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments