Question
A node is balanced if all of its subtrees are of the same size. The size of the subtree is the number of nodes it
A node is balanced if all of its subtrees are of the same size. The size of the subtree is the number of nodes it contains.
The tree above has five balanced nodes marked in gray. The root is not balanced because its subtrees are of different sizes: the left subtree's size equals 3 and the right subtree's size equals 2.
Write a function:
def solution (tree)
that, given a tree, returns the number of balanced nodes it contains.Technical details:
Assume that the following declarations are given:
from dataclasses import dataclass, field
@dataclass
class Node:
subtrees:"array"=field(default__factory=list)A tree is specified using a pointer data structure. For each Node the attribute subtrees hold pointers to its subtrees.
Assume that:
• number of nodes is an integer within the range [1.100].
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
Template given:
#you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")from extratypes import Node
# library with types used in the task
def solution(tree):
# Implement your solution here
pass
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started