<分区>
给定一个二叉树,编写一个函数来检查给定的二叉树是否是完全二叉树。
完全二叉树是一棵二叉树,其中除了可能的最后一层外,每一层都被完全填充,并且所有节点都尽可能地靠左。来源:wikipedia
My approach is do BFS using queue and count the no of nodes. Run a loop till the queue is not null but break once you find one of the below condition holds good:
- left node is not present for a node
- left node is present but right node is not present.
Now we can compare the count that we get from the above approach and the original count of the nodes in the tree. If both equal then complete binary tree else not.
请告诉我这个方法是否正确。谢谢。
这个问题和this的问题是一样的.但我想在这里验证我的方法。
编辑: 该算法由下面的@Boris Strandjev验证。我觉得这是 net 中可用的一些算法中最容易实现的算法。如果您不同意我的主张,我深表歉意。