algorithm - Whether a given Binary Tree is complete or not -


given binary tree, write function check whether given binary tree complete binary tree or not.

a complete binary tree binary tree in every level, except possibly last, filled, , nodes far left possible. source: wikipedia

my approach bfs using queue , count no of nodes. run loop till queue not null break once find 1 of below condition holds good:

  1. left node not present node
  2. left node present right node not present.

now can compare count above approach , original count of nodes in tree. if both equal complete binary tree else not.

please tell me whether approach correct or not. thanks.

this question same of this. wan verify method here.

edit: algorithm verified @boris strandjev below. felt easiest algorithm implement out of algorithms available in net. sincere apologize if not agree assertion.

your algorithm should solve problem.

what doing bfs entirely equivalent drawing tree , tracing nodes finger top-down , left-right. first time can not continue stop tracing finger. if have not counted nodes structure not expected obviously.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -