How to determine whether a binary tree is complete?

前端 未结 16 1368
渐次进展
渐次进展 2021-01-02 19:15

A complete binary tree is defined as a binary tree in which every level, except possibly the deepest, is completely filled. At deepest level, all nodes must be as far left a

16条回答
  •  孤街浪徒
    2021-01-02 20:01

    The simplest procedure is:

    1. Find depth of the tree (simple algorithm).
    2. Count the number of nodes in a tree (by traversing and increasing the counter by one on visiting any node).
    3. For a complete binary tree of level d number of nodes equals to pow(2,d+1)-1.

    If condition satisfy tree, is complete binary tree, else not.

    That's a simple algorithm and turning it into a working code shouldn't be a problem if you are good enough coder.

提交回复
热议问题