Data Structure & Algorithm: Level-1 Problem #37. Perfect Binary Tree

A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level. An example of a perfect binary tree is the (non-incestuous) ancestry chart of a person to a given depth, as each person has exactly two biological parents (one mother and one father).

Given a binary tree, find out whether it’s a perfect one or not.

Sample Input:

root = [1, 2, 3, 4, 5, 6, 7]

Sample Output:

True

Perfect Binary Tree


数据结构和算法:初级练习题 #37 – 完美二叉树


一个深度为 k (>=-1)且有2(k+1) – 1个结点的二叉树称为完美二叉树。换言之:一个二叉树如果所有内部节点都有两个子节点,而且所有叶子深度都相同,那么它就是一颗完美二叉树。

给定一颗二叉树,检查它是否是一颗完美二叉树。

输入样例:

root = [1, 2, 3, 4, 5, 6, 7]

输出样例:

True


Python Solution

Perfect Binary Tree