Skip to content

Commit cd93b4e

Browse files
committed
Modified the circumference program
1 parent 9dc2118 commit cd93b4e

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

BinaryTreeAlgorithms/binarytree.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ struct tree* findLargestBSTSubtree(struct tree *root) {
433433
return largestBST;
434434
}
435435

436+
/* Find the nodes along the circumference of the tree . Select first and last nodes in each level followed by choosing the leaf nodes at each level */
436437

437438
void findCircumference(struct tree *root)
438439
{
@@ -449,6 +450,9 @@ void findCircumference(struct tree *root)
449450
{
450451
itr= s1.top();
451452
s1.pop();
453+
454+
//Concept of same two stack used for zig zag traversal
455+
452456
if(itr->left !=NULL)
453457
{
454458
s2.push(itr->left);
@@ -459,9 +463,10 @@ void findCircumference(struct tree *root)
459463
s2.push(itr->right);
460464
++nextLevel;
461465
}
462-
if((itr->left == NULL && itr->right == NULL) && (count != 0) && (count != currLevel))
463-
printf("\t%d", itr->data);
464-
if(count == 0 || count == currLevel-1)
466+
467+
//Check for node at the circumference
468+
469+
if((itr->left == NULL && itr->right == NULL) || (count != 0) || (count != currLevel-1))
465470
printf("\t%d", itr->data);
466471
++count;
467472
if(s1.empty())

0 commit comments

Comments
 (0)