Created
August 21, 2015 18:03
-
-
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.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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