Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A binary tree is called perfect if all of its nodes have exactly 0 or 2 children and all of its leaves are at the
A binary tree is called perfect if all of its nodes have exactly or children and all of its leaves are at the same level. By the size of a perfect tree we mean its number of nodes.
Write a function:
Class solutionpublic int soultionTree T;size of the biggest perfect subtree that can be obtained by removing nodes. For example, given tree T as shown in the previous figure, the funct i should return as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range ;
the height of tree T number of edges on the longest path from root to leaf is within the range
Technical details
A binary tree can be specified using a pointer data structure. Assume that the following declarations are given:
class Tree
public int x;
public Tree l;
public Tree r;
An empty tree is represented by an empty pointer denoted by null A nonempty tree is represented by a pointer to an object representing its root. The attribute holds the integer contained in the root, whereas attributes and r hold the left and right subtrees of the binary tree, respectively.
For the purpose of entering your own test cases, you can denote a tree recursively in the following way. An empty binary tree is denoted by None. A nonempty tree is denoted as X L R where X is the value contained in the root and L and R denote the left and right subtrees, respectively. The tree from the above figure can be denoted as:
None, None, None None, None None, None None, None None, None
None
Write java solution
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