Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Count Sum of binary search tree node using queue data structure ( iterative approach )

Time complexity of this program O(n)

Accepted Output

Given BST tree node is:

475, 409, 510, 421, 544, 526, 564, 541, 558, 576

View Tree Click Here

475409510544564526576421558541

Result :

Sum of node is : [409 + 421 + 475 + 510 + 526 + 541 + 544 + 558 + 564 + 576 ] = 5124

Another Example

Given BST tree node is:

50, 90, 30, 90, 40, 10, 15, 10, 80

View Tree Click Here

509030904010151080

Result :

Sum of node is : [50 + 30 + 90 + 10 + 40 + 80 + 90 + 15 + 10] = 415

Code execution:

Given binary search tree Sum of binary search tree nodes 1509030904010151080

Given below code execution process

Code execution process Code execution process to sum all bst nodes Stack Areamainqueue(pointer)root(pointer)enqueuehelp(pointer)new_node(pointer)queue(pointer)result (int) =150start(pointer)temp(pointer) Heap Areastruct Treedata (int)= 150left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 90left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 30left_child (pointer) right_child (pointer) struct Treedata (int)= 90left_child (pointer) right_child (pointer)= NULLstruct Treedata (int)= 40left_child (pointer)= NULLright_child (pointer) struct Treedata (int)= 10left_child (pointer) right_child (pointer) struct Treedata (int)= 15left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 10left_child (pointer)= NULLright_child (pointer)= NULLstruct Treedata (int)= 80left_child (pointer)= NULLright_child (pointer)= NULLstruct Queuenode (pointer) next (pointer)= NULLstruct Queuenode (pointer)= ? next (pointer)= NULL

All execution steps are not showing here. Click below link and visualize all process like how to create binary search tree, printing that data, and implement queue of this binary search tree, And sum of all nodes.

Click To Visualize Code

C program to sum of binary search tree(BST) nodes. Iterative approach.

Output
  BST Data is :  10  10  15  30  40  80  90  90  150
 Sum of Node is [515]
 Free Memory of Queue

 Free BST node

  

Spread the post