Skip to main content

Check if all leaves are at same level

Suppose that given binary tree are contains N nodes. Check if programmatically, on this binary tree all leaf nodes are exist in a same level. For example.

Check if all leaves are at same level

In the context of a tree data structure, "leaves" refer to the nodes that do not have any children i.e., nodes that are at the bottom level of the tree.

When someone says "check if all leaves are at the same level", it means to determine whether all the leaves of a given tree are at the same distance from the root node. In other words, if you traverse the tree from the root node to any leaf node, the length of the path should be the same for all leaf nodes.

If all the leaves are at the same level, then the tree is said to be "balanced". If the leaves are at different levels, then the tree is said to be "unbalanced".

Checking whether all leaves are at the same level can be done through a traversal algorithm such as depth-first search or breadth-first search. By keeping track of the depth of each leaf node during traversal, we can compare their depths at the end to determine whether they are at the same level.

Code solution


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