What must be the missing logic in place of missing lines for finding sum of nodes of binary tree in alternate levels?
//e.g:-consider -complete binary tree:-height-3, [1,2,3,4,5,6,7]-answer must be 23
n=power(2,height)-1; //assume input is height and a[i] contains tree elements
for(i=1;i<=n;)
{
//present level is initialized to 1 and sum is initialized to 0
for(j=1;j<=pow(2,currentlevel-1);j++)
{
sum=sum+a[i];
i=i+1;
}
//missing logic
}
//e.g:-consider -complete binary tree:-height-3, [1,2,3,4,5,6,7]-answer must be 23
n=power(2,height)-1; //assume input is height and a[i] contains tree elements
for(i=1;i<=n;)
{
//present level is initialized to 1 and sum is initialized to 0
for(j=1;j<=pow(2,currentlevel-1);j++)
{
sum=sum+a[i];
i=i+1;
}
//missing logic
}A.
i=i+pow(2,currentlevel);
currentlevel=currentlevel+2;
j=1;B.
i=i+pow(2,currentlevel);
currentlevel=currentlevel+2;
j=0;C.
i=i-pow(2,currentlevel);
currentlevel=currentlevel+2;
j=1;D.
i=i+pow(2,currentlevel);
currentlevel=currentlevel+1;
j=1;Answer: Option A
Related Questions on Binary Search Trees(B Tree)
A. O(1)
B. O(log n)
C. O(n)
D. O(n log n)
Which traversal method of a BST will produce a sorted sequence of node values?
A. Inorder
B. Preorder
C. Postorder
D. Level-order
What is the maximum number of children a node in a Binary Search Tree (BST) can have?
A. 1
B. 2
C. 3
D. Any number
How can you determine if a Binary Tree is a Binary Search Tree (BST)?
A. Verify if all nodes in the left subtree are less than the root and all nodes in the right subtree are greater than the root.
B. Check if the tree is balanced.
C. Ensure all nodes have exactly two children.
D. Verify the height of the tree.

Join The Discussion