Skip to content

Commit 742ae2e

Browse files
committed
110
1 parent 8cccb1d commit 742ae2e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

ruby/110-Balanced-Binary-Tree.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def is_balanced(root)
2+
$balance = true
3+
balanced?(root)
4+
$balance
5+
end
6+
7+
def balanced?(root)
8+
return -1 if root.nil?
9+
10+
left_height = 1 + balanced?(root.left)
11+
right_height = 1 + balanced?(root.right)
12+
$balance = false if (left_height - right_height).abs > 1
13+
14+
left_height > right_height ? left_height : right_height
15+
end

0 commit comments

Comments
 (0)