Function to find the size of Binary Tree | Gyaneshwar pardhi
1.using recursion: size of binary tree =(size of left child+1+ size of right child).
2.Non-recursive approach
Read full article from Function to find the size of Binary Tree | Gyaneshwar pardhi
1.using recursion: size of binary tree =(size of left child+1+ size of right child).
2.Non-recursive approach
static
int
sizeOfBinaryTreeWithoutRecursion(treeNode * rootNode) {
int
count = 0;
// create a Q which store pointer of treeNode object
queue<treeNode*> Q;
//add first element to Q
Q.push(rootNode);
while
(!Q.empty()) {
//till queue is not empty
treeNode * temp = Q.front();
Q.pop();
count++;
//increase count by 1
if
(temp->leftChild)
//if node have left child then add it to Q
Q.push(temp->leftChild);
if
(temp->rightChild)
//if node have right child then add it to Q
Q.push(temp->rightChild);
}
return
count;
//size of tree
}