Height = log(n + 1) - 1 where n is count of nodes.
Can be represented using an array, where the left child of a node at index i is stored at index 2i+1 and the right child is stored at index 2i+2. This makes it easy to access the children of a node and to traverse the tree.
Given a Binary Tree, the task is to check whether the given Binary Tree is a perfect Binary Tree or not.
[Naive Method] Two Traversals - O(n) Time and O(h) Space
The idea is to first find the depth of the binary tree. Then recursively check whether all leaf nodes are present at the same depth and every internal node has exactly two children. If any node violates these conditions, the tree is not a perfect binary tree.
Output
True
[Expected Approach] Single DFS Traversal - O(n) Time and O(h) Space
The idea is to recursively traverse the tree and store the level of the first leaf node. Then, check whether all other leaf nodes are at the same level and every non-leaf node has exactly two children. If both conditions are satisfied, the tree is a perfect binary tree.