Skip to content

Commit

Permalink
Use built-in max for diameter problem
Browse files Browse the repository at this point in the history
  • Loading branch information
arunsathiya committed Feb 27, 2024
1 parent c38cbec commit e55ca9a
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/543-diameter-of-binary-tree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,17 @@ type TreeNode struct {
}

func diameterOfBinaryTree(root *TreeNode) int {
max := 0
maxD := 0
var depth func(node *TreeNode) int
depth = func(node *TreeNode) int {
if node == nil {
return 0
}
leftDepth := depth(node.Left)
rightDepth := depth(node.Right)
max = maxHelper(max, leftDepth+rightDepth)
return 1 + maxHelper(leftDepth, rightDepth)
maxD = max(maxD, leftDepth+rightDepth)
return 1 + max(leftDepth, rightDepth)
}
depth(root)
return max
}

func maxHelper(a, b int) int {
if a > b {
return a
}
return b
return maxD
}

0 comments on commit e55ca9a

Please sign in to comment.