Answer to Question #239854 in Python for food_calc

Question #239854

program for greedy's algorithm fill the remaining code

def printPath(root, sum): 


def getRootToLeafSum(root):

 

def findMaxSumPath(root):

  sum = getRootToLeafSum(root)

  print("The maximum sum is:", sum)

  print("The maximum sum path is :", end=' ')

  printPath(root, sum)

def Rnode():

  height=int(input("Enter the level of Binary tree:"))

  p=int(input("Perfect=1,imperfect=0:"))

  try: 

    B=tree(height,is_perfect=p)

    B=list(B)[0]

    print(B)

    findMaxSumPath(B)

  except:

    print("level of Binary value between 0-9")

# This main method of given program to call Rnode() function

if _name_ == '__main__': Rnode()

ONLY THING YOU HAVE TO WRITE THE REMAINING CODE getRootToLeafSum(root) and  printPath(root, sum)


1
Expert's answer
2021-09-20T23:53:19-0400
def printPath(root, sum):
    if sum == 0:
        return True
    if root is None:
        return False
    left = printPath(root.left, sum - root.data)
    right = printPath(root.right, sum - root.data)
    if left or right:
        print(root.data, end=' ')
 
    return left or right




def getRootToLeafSum(root):
   
    if root is None:
        return 0
 
    left = getRootToLeafSum(root.left)
    right = getRootToLeafSum(root.right)
    return (left if left > right else right) + root.data
 


 


def findMaxSumPath(root):


  sum = getRootToLeafSum(root)


  print("The maximum sum is:", sum)


  print("The maximum sum path is :", end=' ')


  printPath(root, sum)


def Rnode():


  height=int(input("Enter the level of Binary tree:"))


  p=int(input("Perfect=1,imperfect=0:"))


  try: 


    B=tree(height,is_perfect=p)


    B=list(B)[0]


    print(B)


    findMaxSumPath(B)


  except:


    print("level of Binary value between 0-9")


# This main method of given program to call Rnode() function


if __name__ == '__main__': Rnode()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS