Skip to content

Commit

Permalink
543
Browse files Browse the repository at this point in the history
  • Loading branch information
TedTran2019 committed Jul 8, 2022
1 parent 742ae2e commit ca97a48
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ruby/543-Diameter-of-Binary-Tree.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def diameter_of_binary_tree(root)
$max = 0
max_height = diameter(root)
$max > max_height ? $max : max_height
end

def diameter(root)
return -1 if root.nil?

left = 1 + diameter(root.left)
right = 1 + diameter(root.right)
diameter = left + right
$max = diameter if diameter > $max

left > right ? left : right
end

0 comments on commit ca97a48

Please sign in to comment.