Posted on by Kalkicode
Code Binary Tree

Check whether a given binary tree is perfect or not

In computer science and binary tree data structures, a perfect binary tree is a binary tree where all of the non-leaf nodes have exactly two children and all the leaves are at the same level. In other words, a perfect binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

Example of perfect binary tree

Therefore, to check whether a given binary tree is perfect or not, you need to verify if the tree satisfies the following conditions:

  1. All non-leaf nodes have exactly two children.
  2. All the leaves are at the same level, i.e., they have the same depth.
  3. Every level, except possibly the last, is completely filled, i.e., there are no missing nodes.

If all these conditions are true, the binary tree is perfect, otherwise, it is not perfect.

Program List

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment