• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
June 02, 2022 |1.5K Views

Node having maximum sum of immediate children and itself in n-ary tree

  Share   Like
Description
Discussion

Given an N-Ary tree, find and return the node for which sum of data of all children and the node itself is maximum. In the sum, data of node itself and data of its immediate children is to be taken.

The idea is we will maintain a integer variable maxsum which contains the maximum sum yet, and a resnode node pointer which points to the node with maximum sum. 
Traverse the tree and maintain the sum of root and data of all its immediate children in currsum 
integer variable and update the maxsum variable accordingly.

Node having maximum sum of immediate children and itself in n-ary tree : https://www.geeksforgeeks.org/node-maximum-sum-immediate-children-n-ary-tree/