max sum of child of immediate node

 TreeNode<int>* maxSumNode(TreeNode<int>* root) {

    // Write your code here

    int sum=0;

    //TreeNode<int>* max=new TreeNode<int>(-999);

    for(int i=0;i<root->children.size();i++){

        sum+=root->children[i]->data;

    }

    TreeNode<int>*max=new TreeNode<int>(sum);

    

    for(int i=0;i<root->children.size();i++){

        

        TreeNode<int>* a=maxSumNode(root->children[i]);

        if(a->data>max->data){

            max=a;

        }

    }

    return max;

    

    

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings