Skip to content

Instantly share code, notes, and snippets.

@mattantonelli
Created August 21, 2015 18:03
Show Gist options
  • Select an option

  • Save mattantonelli/81a9fb3bb113e2de20ec to your computer and use it in GitHub Desktop.

Select an option

Save mattantonelli/81a9fb3bb113e2de20ec to your computer and use it in GitHub Desktop.
Checks if a binary tree's children all contain 2 nodes (except the last level.)
#!/usr/bin/env ruby
# encoding: utf-8
# Task: You are given number of nodes (n) in a binary tree.
# You have to tell whether it is possible having a tree with given number of nodes
# such that all nodes at all levels have 2 children (except the last level).
num_nodes = ARGV[0].to_i
total = 0
row = 0
while total < num_nodes
total = total + (1 << row)
row += 1
end
puts total == num_nodes ? 'Yes' : 'No'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment