programming-examples/ruby/Algorithms/valid_binary_tree.rb

24 lines
472 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
MAX_INT = 99_999_999_999_999_999_999
LOW_INT = -99_999_999_999_999_999_999
def valid_binary_tree(root)
stack = [[root, LOW_INT, MAX_INT]]
while stack.length > 0
node, low_bounds, high_bounds = stack.pop
if node.val < low_bounds || node.val > high_bounds
return false
end
if node.left
stack.push([node.left, low_bounds, node.val])
end
if node.right
stack.push([node.right, node.val, high_bounds])
end
end
true
end