Maximum parent children sum in Binary tree Last Updated : 27 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Given a Binary Tree, find the maximum sum in a binary tree by adding the parent with its children. Exactly three Node needs to be added. If the tree does not have a node with both of its children as not NULL, return 0. We simply traverse the tree and find the Node that has the maximum sum. We need to take care of the leaves. Implementation: C++ // C++ program to find maximum sum of a node // and its children #include <iostream> using namespace std; struct Node { int data; struct Node *left, *right; }; // insertion of Node in Tree struct Node* newNode(int n) { struct Node* root = new Node(); root->data = n; root->left = root->right = NULL; return root; } int maxSum(struct Node* root) { if (root == NULL) return 0; int res = maxSum(root->left); // if left and right link are null then // add all the three Node if (root->left != NULL && root->right != NULL) { int sum = root->data + root->left->data + root->right->data; res = max(res, sum); } return max(res, maxSum(root->right)); } int main() { struct Node* root = newNode(15); root->left = newNode(16); root->left->left = newNode(8); root->left->left->left = newNode(55); root->left->right = newNode(67); root->left->right->left = newNode(44); root->right = newNode(17); root->right->left = newNode(7); root->right->left->right = newNode(11); root->right->right = newNode(41); cout << maxSum(root); return 0; } Java // Java program to find // maximum sum of a node // and its children import java.util.*; // insertion of Node in Tree class Node { int data; Node left, right; Node(int key) { data = key; left = right = null; } } class GFG { public static int maxSum(Node root) { if (root == null) return 0; int res = maxSum(root.left); // if left and right link are null // then add all the three Node if (root.left != null && root.right != null) { int sum = root.data + root.left.data + root.right.data; res = Math.max(res, sum); } return Math.max(res, maxSum(root.right)); } // Driver code public static void main (String[] args) { Node root = new Node(15); root.left = new Node(16); root.left.right = new Node(67); root.left.right.left = new Node(44); root.left.left = new Node(8); root.left.left.left = new Node(55); root.right = new Node(17); root.right.right = new Node(41); root.right.left = new Node(7); root.right.left.right = new Node(11); System.out.print(maxSum(root)); } } // This code is contributed // by akash1295 Python3 # Python program to find maximum # sum of a node and its children class newNode(): def __init__(self, data): self.data = data self.left = None self.right = None def maxSum(root): if (root == None): return 0 res = maxSum(root.left) # if left and right link are None then # add all the three Node if (root.left != None and root.right != None): sum = root.data + root.left.data + root.right.data res = max(res, sum) return max(res, maxSum(root.right)) # Driver code if __name__ == '__main__': root = newNode(15) root.left = newNode(16) root.left.left = newNode(8) root.left.left.left = newNode(55) root.left.right = newNode(67) root.left.right.left = newNode(44) root.right = newNode(17) root.right.left = newNode(7) root.right.left.right = newNode(11) root.right.right = newNode(41) print(maxSum(root)) # This code is contributed by SHUBHAMSINGH10 C# // C# program to find // maximum sum of a node // and its children using System; // insertion of Node in Tree public class Node { public int data; public Node left, right; public Node(int key) { data = key; left = right = null; } } public class GFG { public static int maxSum(Node root) { if (root == null) return 0; int res = maxSum(root.left); // if left and right link are null // then add all the three Node if (root.left != null && root.right != null) { int sum = root.data + root.left.data + root.right.data; res = Math.Max(res, sum); } return Math.Max(res, maxSum(root.right)); } // Driver code public static void Main () { Node root = new Node(15); root.left = new Node(16); root.left.right = new Node(67); root.left.right.left = new Node(44); root.left.left = new Node(8); root.left.left.left = new Node(55); root.right = new Node(17); root.right.right = new Node(41); root.right.left = new Node(7); root.right.left.right = new Node(11); Console.Write(maxSum(root)); } } /* This code is contributed PrinciRaj1992 */ JavaScript <script> // Javascript program to find // maximum sum of a node // and its children // Insertion of Node in Tree class Node { constructor(key) { this.data = key; this.left = null; this.right = null; } } function maxSum(root) { if (root == null) return 0; var res = maxSum(root.left); // If left and right link are null // then add all the three Node if (root.left != null && root.right != null) { var sum = root.data + root.left.data + root.right.data; res = Math.max(res, sum); } return Math.max(res, maxSum(root.right)); } // Driver code var root = new Node(15); root.left = new Node(16); root.left.right = new Node(67); root.left.right.left = new Node(44); root.left.left = new Node(8); root.left.left.left = new Node(55); root.right = new Node(17); root.right.right = new Node(41); root.right.left = new Node(7); root.right.left.right = new Node(11); document.write(maxSum(root)); // This code is contributed by rutvik_56 </script> Output91 Time Complexity: O(N) where N is the number of nodes in given binary tree.Auxiliary Space: O(h) where h is the height of binary tree due to recursion call. Comment More infoAdvertise with us Next Article Maximum parent children sum in Binary tree M Mohd_Saliem Follow Improve Article Tags : Misc Tree DSA Binary Tree tree-traversal +1 More Practice Tags : MiscTree Similar Reads Maximum Path Sum in a Binary Tree Given a binary tree, the task is to find the maximum path sum. The path may start and end at any node in the tree.Example: Input: Output: 42Explanation: Max path sum is represented using green colour nodes in the above binary tree.Input: Output: 31Explanation: Max path sum is represented using green 8 min read Maximum spiral sum in Binary Tree Given a binary tree containing n nodes. The task is to find the maximum sum obtained when the tree is spirally traversed. In spiral traversal one by one all levels are being traversed with the root level traversed from right to left, then the next level from left to right, then further next level fr 9 min read Find maximum level sum in Binary Tree Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it. Examples: Input : 4 / \ 2 -5 / \ /\ -1 3 -2 6Output: 6Explanation :Sum of all nodes of 0'th level is 4Sum of all nodes of 1'th level is -3Sum of all nodes of 0'th level is 6Hence maximum sum is 6 15+ min read Maximum Path sum in a N-ary Tree Given an undirected tree with n nodes numbered from 1 to n and an array arr[] where arr[i] denotes the value assigned to (i+1)th node. The connections between the nodes are provided in a 2-dimensional array edges[][]. The task is to find the maximum path sum between any two nodes. (Both the nodes ca 7 min read Find maximum vertical sum in binary tree Given a binary tree, find the maximum vertical level sum in binary tree.Examples: Input : 3 / \ 4 6 / \ / \ -1 -2 5 10 \ 8 Output : 14Vertical level having nodes 6 and 8 has maximumvertical sum 14. Input : 1 / \ 5 8 / \ \ 2 -6 3 \ / -1 -4 \ 9Output : 4 A simple solution is to first find vertical lev 9 min read Like