Question: (Complete in Python ) Complete the function isBalanced() to take in a root node of a binary tree and return True if the tree is

(Complete in Python )

Complete the function isBalanced() to take in a root node of a binary tree and return True if the tree is balanced, False otherwise.

Let's define the criteria for a tree to be balanced if for every node in the tree, the difference in height between the left subtree and the right subtree of that node is no more than 1.

You will benefit from re-using your code for last week's homework Trees 3 - Height.

class Node: def __init__(self, value): self.value = value self.left = None self.right = None def isBalanced(root): # TODO

root = Node(7) root.left = Node(4) root.left.left = Node(1) root.left.right = Node(5) """ 7 / 4 (NOT BALANCED) / \ 1 5 """ print(isBalanced(root)) # should print False

root.right = Node(10) root.right.right = Node(13) """ 7 / \ 4 10 (BALANCED) / \ \ 1 5 13 """ print(isBalanced(root)) # should print True

root.left.left.left = Node(0) root.left.right = None """ 7 / \ 4 10 (NOT BALANCED) / \ 1 13 / 0 """ print(isBalanced(root)) # should print False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!