Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Alexander Calder invented the mobile in 1931. We define a mobile tree as a binary tree with nodes of the following type: struct Node {int
Alexander Calder invented the mobile in 1931. We define a mobile tree as a binary tree with nodes of the following type: struct Node {int weight?//the weight of this node itself Node* left; Node* right;}; Define the weight of a mobile tree to be the sum of the weights of all the nodes in the tree. (The empty tree therefore has a weight of zero.) We say that a mobile tree is in balance if either: it is empty, or if the weights of its left and right subtrees are equal, and each of those subtrees is itself in balance. This mobile tree, for example, is in balance. Write a function named in Balance that takes a pointer to the root node of a mobile tree and returns true if that mobile tree is in balance, or false otherwise. If you wish, you may write an additional function called by is InBalance. You must not use any global variables, nor any variables of types other than bool, bool&, int, int&, or Node*. You must not use the keywords while, for, goto, or static. Your solution must not be more than 30 lines of code. To not lose significant credit, your solution must be such that a call to isInBalance results in each node in the mobile tree being visited no more than once. Answer: bool isInBalance(Node* root) {
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