You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
507 B
Ruby

def balanced_binary_tree(node)
depths = []
stack = []
stack.push([node, 0])
while stack.length > 0
node, depth = stack.pop
if !node.left && !node.right
unless depths.include?(depth)
depths << depth
if depths.length > 2 || (depths.length == 2 && (depth[0] - depth[1]).abs > 1)
return false
end
end
else
stack.push([node.left, depth + 1]) if node.left
stack.push([node.right, depth + 1]) if node.right
end
end
true
end