What is the value return by the function: sum(struct TreeNode* root) where root is the head pointer of the binary tree passed?

int result = 0;
void fun(struct TreeNode* root, bool flag)
{
if(!root->left && !root->right && flag)
{
 result += root->val;
 return;
}
if(root->left)
fun(root->left, true);
if(root->right)
fun(root->right, false);    
}
int sum(struct TreeNode* root){
if(root == NULL)
return 0; 
fun(root,false);
return result;
}


Definition for a binary tree node.
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

1
sum of all leaf node in binary tree
2
sum of all right leaf node in binary tree
3
sum of all left leaf node in binary tree
4
While throw error or give wrong answer for some test case

Sponsored

hivanix.in

Visit

This quiz is brought to you by hivanix.in

🌐 Web App Development

Quick Navigation