Write a PYTHON program to find the path with the largest sum. Tree can be in N level and draw the binary tree and accept a perfect and imperfect binary tree as an option from the user.
I need a code like this but I need to print a tree and try: B=tree in that tree is undefined
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")
https://www.assignmentexpert.com/homework-answers/programming-and-computer-science/python/question-239854#
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")
def print_tree(nodes):
from binarytree import build
b_tree = build(nodes)
print(b_tree)
# This main method of given program to call Rnode() function
if __name__ == '__main__':
Rnode()
Comments
Leave a comment