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 Treestruct 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 childrenimport java.util.*;Â
// insertion of Node in Treeclass 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 childrenclass 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 childrenusing System;Â
// insertion of Node in Treepublic 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 Treeclass 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 codevar 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> |
91
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.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

