Last active
May 23, 2020 08:10
-
-
Save InBrewJ/14af0d8503dbbc21aca9457d7de04c2f to your computer and use it in GitHub Desktop.
Largest Branch: BST, Iteration!
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
def goLeft(tree): | |
left_sum = 0 | |
power_count = 1 | |
descending = True | |
while descending: | |
for i in range( (2**power_count)-1, (2**(power_count+1))-1 ): | |
level_span = ((2**power_count)-1) | |
range_len = len(range( (2**power_count)-1, (2**(power_count+1))-1)) | |
try: | |
if i < int(level_span + (range_len / 2)): | |
print("Adding: ", tree[i], "to left") | |
left_sum += max(0, tree[i]) | |
except: | |
print('Bottom of the tree!') | |
descending = False | |
return left_sum | |
power_count += 1 | |
return left_sum | |
def goRight(tree): | |
right_sum = 0 | |
descending = True | |
power_count = 1 | |
while descending: | |
for i in range( (2**power_count)-1, (2**(power_count+1))-1 ): | |
level_span = (2**(power_count+1))-1 | |
range_len = len(range( (2**power_count)-1, (2**(power_count+1))-1)) | |
try: | |
if i >= int(level_span - (range_len / 2)): | |
print("Adding: ", tree[i], "to right") | |
right_sum += max(0, tree[i]) | |
except: | |
print('Bottom of the tree!') | |
descending = False | |
return right_sum | |
power_count += 1 | |
return right_sum | |
def solution(arr): | |
if len(arr) > 1: | |
left_val = goLeft(arr) | |
right_val = goRight(arr) | |
if left_val > right_val: | |
return "Left" | |
if right_val > left_val: | |
return "Right" | |
return "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment