We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8cccb1d commit 742ae2eCopy full SHA for 742ae2e
ruby/110-Balanced-Binary-Tree.rb
@@ -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
0 commit comments