Count subtrees that sum up to a given value x only using single recursive function - GeeksforGeeks

27 Dec, 2021
Given a binary tree containing n nodes. The problem is to count subtrees having total node’s data sum equal to a given value using only single recursive functions.
Examples: 
Input : 
             5
           /   \  
        -10     3
        /  \   /  \
       9    8 -4   7
       
       x = 7

Output : 2
There are 2 subtrees with sum 7.

1st one is leaf node:
7.

2nd one is:

      -10
     /   \
    9     8
Source: Microsoft Interview Experience | Set 157.
Approach: 
countSubtreesWithSumX(root, count, x)
    if !root then
        return 0
        
    ls = countSubtreesWithSumX(root->left, count, x)
    rs = countSubtreesWithSumX(root->right, count, x)
    sum = ls + rs + root->data
    
    if sum == x then
    count++
    return sum

countSubtreesWithSumXUtil(root, x)
    if !root then
        return 0
    
    Initialize count = 0
    ls = countSubtreesWithSumX(root->left, count, x)
    rs = countSubtreesWithSumX(root->right, count, x)
    
    if (ls + rs + root->data) == x
        count++
    return count
C++
Java
Python3
C#
Javascript
// C++ implementation to count subtrees that// sum up to a given value x#include <bits/stdc++.h> using namespace std; // structure of a node of binary treestruct Node {    int data;    Node *left, *right;}; // function to get a new nodeNode* getNode(int data){    // allocate space    Node* newNode = (Node*)malloc(sizeof(Node));     // put in the data    newNode->data = data;    newNode->left = newNode->right = NULL;    return newNode;} // function to count subtrees that// sum up to a given value xint countSubtreesWithSumX(Node* root,                          int& count, int x){    // if tree is empty    if (!root)        return 0;     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root->left, count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root->right, count, x);     // sum of nodes in the subtree rooted    // with 'root->data'    int sum = ls + rs + root->data;     // if true    if (sum == x)        count++;     // return subtree's nodes sum    return sum;} // utility function to count subtrees that// sum up to a given value xint countSubtreesWithSumXUtil(Node* root, int x){    // if tree is empty    if (!root)        return 0;     int count = 0;     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root->left, count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root->right, count, x);     // if tree's nodes sum == x    if ((ls + rs + root->data) == x)        count++;     // required count of subtrees    return count;} // Driver program to test aboveint main(){    /* binary tree creation                   5              /   \            -10     3           /  \   /  \          9    8 -4   7    */    Node* root = getNode(5);    root->left = getNode(-10);    root->right = getNode(3);    root->left->left = getNode(9);    root->left->right = getNode(8);    root->right->left = getNode(-4);    root->right->right = getNode(7);     int x = 7;     cout << "Count = "         << countSubtreesWithSumXUtil(root, x);     return 0;}
// Java program to find if// there is a subtree with// given sumimport java.util.*;class GFG{ // structure of a node// of binary treestatic class Node{    int data;    Node left, right;} static class INT{    int v;    INT(int a)    {        v = a;    }} // function to get a new nodestatic Node getNode(int data){    // allocate space    Node newNode = new Node();     // put in the data    newNode.data = data;    newNode.left = newNode.right = null;    return newNode;} // function to count subtrees that// sum up to a given value xstatic int countSubtreesWithSumX(Node root,                          INT count, int x){    // if tree is empty    if (root == null)        return 0;     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root.left,                                   count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root.right,                                   count, x);     // sum of nodes in the subtree    // rooted with 'root.data'    int sum = ls + rs + root.data;     // if true    if (sum == x)        count.v++;     // return subtree's nodes sum    return sum;} // utility function to// count subtrees that// sum up to a given value xstatic int countSubtreesWithSumXUtil(Node root,                                     int x){    // if tree is empty    if (root == null)        return 0;     INT count = new INT(0);     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root.left,                                   count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root.right,                                   count, x);     // if tree's nodes sum == x    if ((ls + rs + root.data) == x)        count.v++;     // required count of subtrees    return count.v;} // Driver Codepublic static void main(String args[]){    /* binary tree creation                    5            / \        -10     3        / \ / \        9 8 -4 7    */    Node root = getNode(5);    root.left = getNode(-10);    root.right = getNode(3);    root.left.left = getNode(9);    root.left.right = getNode(8);    root.right.left = getNode(-4);    root.right.right = getNode(7);     int x = 7;     System.out.println("Count = " +           countSubtreesWithSumXUtil(root, x));}} // This code is contributed// by Arnab Kundu
# Python3 implementation to count subtrees# that Sum up to a given value x # class to get a new nodeclass getNode:    def __init__(self, data):                 # put in the data        self.data = data        self.left = self.right = None         # function to count subtrees that# Sum up to a given value xdef countSubtreesWithSumX(root, count, x):         # if tree is empty    if (not root):        return 0     # Sum of nodes in the left subtree    ls = countSubtreesWithSumX(root.left,                               count, x)     # Sum of nodes in the right subtree    rs = countSubtreesWithSumX(root.right,                               count, x)     # Sum of nodes in the subtree    # rooted with 'root.data'    Sum = ls + rs + root.data     # if true    if (Sum == x):        count[0] += 1     # return subtree's nodes Sum    return Sum # utility function to count subtrees# that Sum up to a given value xdef countSubtreesWithSumXUtil(root, x):         # if tree is empty    if (not root):        return 0     count = [0]     # Sum of nodes in the left subtree    ls = countSubtreesWithSumX(root.left,                               count, x)     # Sum of nodes in the right subtree    rs = countSubtreesWithSumX(root.right,                               count, x)     # if tree's nodes Sum == x    if ((ls + rs + root.data) == x):        count[0] += 1     # required count of subtrees    return count[0] # Driver Codeif __name__ == '__main__':         # binary tree creation        #         5    #         / \    #     -10     3    #     / \ / \    #     9 8 -4 7    root = getNode(5)    root.left = getNode(-10)    root.right = getNode(3)    root.left.left = getNode(9)    root.left.right = getNode(8)    root.right.left = getNode(-4)    root.right.right = getNode(7)     x = 7     print("Count =",           countSubtreesWithSumXUtil(root, x)) # This code is contributed by PranchalK
using System; // c# program to find if// there is a subtree with// given sumpublic class GFG{ // structure of a node// of binary treepublic class Node{    public int data;    public Node left, right;} public class INT{    public int v;    public INT(int a)    {        v = a;    }} // function to get a new nodepublic static Node getNode(int data){    // allocate space    Node newNode = new Node();     // put in the data    newNode.data = data;    newNode.left = newNode.right = null;    return newNode;} // function to count subtrees that// sum up to a given value xpublic static int countSubtreesWithSumX(Node root,                                    INT count, int x){    // if tree is empty    if (root == null)    {        return 0;    }     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root.left, count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root.right, count, x);     // sum of nodes in the subtree    // rooted with 'root.data'    int sum = ls + rs + root.data;     // if true    if (sum == x)    {        count.v++;    }     // return subtree's nodes sum    return sum;} // utility function to// count subtrees that// sum up to a given value xpublic static int countSubtreesWithSumXUtil(Node root, int x){    // if tree is empty    if (root == null)    {        return 0;    }     INT count = new INT(0);     // sum of nodes in the left subtree    int ls = countSubtreesWithSumX(root.left, count, x);     // sum of nodes in the right subtree    int rs = countSubtreesWithSumX(root.right, count, x);     // if tree's nodes sum == x    if ((ls + rs + root.data) == x)    {        count.v++;    }     // required count of subtrees    return count.v;} // Driver Codepublic static void Main(string[] args){    /* binary tree creation                    5            / \        -10     3        / \ / \        9 8 -4 7    */    Node root = getNode(5);    root.left = getNode(-10);    root.right = getNode(3);    root.left.left = getNode(9);    root.left.right = getNode(8);    root.right.left = getNode(-4);    root.right.right = getNode(7);     int x = 7;     Console.WriteLine("Count = " + countSubtreesWithSumXUtil(root, x));}} // This code is contributed by Shrikant13
<script>    // Javascript program to find if    // there is a subtree with given sum         class Node    {        constructor(data) {           this.left = null;           this.right = null;           this.data = data;        }    }         let v;         // function to get a new node    function getNode(data)    {        // allocate space        let newNode = new Node(data);        return newNode;    }     // function to count subtrees that    // sum up to a given value x    function countSubtreesWithSumX(root, x)    {        // if tree is empty        if (root == null)            return 0;         // sum of nodes in the left subtree        let ls = countSubtreesWithSumX(root.left, x);         // sum of nodes in the right subtree        let rs = countSubtreesWithSumX(root.right, x);         // sum of nodes in the subtree        // rooted with 'root.data'        let sum = ls + rs + root.data;         // if true        if (sum == x)            v++;         // return subtree's nodes sum        return sum;    }     // utility function to    // count subtrees that    // sum up to a given value x    function countSubtreesWithSumXUtil(root, x)    {        // if tree is empty        if (root == null)            return 0;         v = 0;         // sum of nodes in the left subtree        let ls = countSubtreesWithSumX(root.left, x);         // sum of nodes in the right subtree        let rs = countSubtreesWithSumX(root.right, x);         // if tree's nodes sum == x        if ((ls + rs + root.data) == x)            v++;         // required count of subtrees        return v;    }         /* binary tree creation                   5            / \        -10     3        / \ / \        9 8 -4 7    */    let root = getNode(5);    root.left = getNode(-10);    root.right = getNode(3);    root.left.left = getNode(9);    root.left.right = getNode(8);    root.right.left = getNode(-4);    root.right.right = getNode(7);      let x = 7;      document.write("Count = " +           countSubtreesWithSumXUtil(root, x)); // This code is contributed by decode2207.</script>
Output: 
Count = 2
Time Complexity: O(n).
Another Approach: 
countSubtreesWithSumXUtil(root, x)

 Initialize static count = 0
 Initialize static *ptr = root
    if !root then
        return 0
    
    Initialize static count = 0
    ls+ = countSubtreesWithSumXUtil(root->left, count, x)
    rs+ = countSubtreesWithSumXUtil(root->right, count, x)
    
    if (ls + rs + root->data) == x
        count++
    
    if(ptr!=root)
       return ls + root->data + rs
    else
    return count
C++
Java
Python3
C#
Javascript
// C++ program to find if// there is a subtree with// given sum#include <bits/stdc++.h> using namespace std; // Structure of a node of binary treestruct Node {    int data;    Node *left, *right;}; // Function to get a new nodeNode* getNode(int data){    // Allocate space    Node* newNode = (Node*)malloc(sizeof(Node));     // Put in the data    newNode->data = data;    newNode->left = newNode->right = NULL;    return newNode;}    // Utility function to count subtrees that// sum up to a given value xint countSubtreesWithSumXUtil(Node* root, int x){    static int count=0;    static Node* ptr=root;    int l=0,r=0;    if(root==NULL)    return 0;         l+=countSubtreesWithSumXUtil(root->left,x);         r+=countSubtreesWithSumXUtil(root->right,x);     if(l+r+root->data==x)    count++;     if(ptr!=root)        return l+root->data+r;         return count;              } // Driver codeint main(){    /* binary tree creation              5            /  \          -10   3          / \  / \          9 8 -4 7    */    Node* root = getNode(5);    root->left = getNode(-10);    root->right = getNode(3);    root->left->left = getNode(9);    root->left->right = getNode(8);    root->right->left = getNode(-4);    root->right->right = getNode(7);     int x = 7;     cout << "Count = "        << countSubtreesWithSumXUtil(root, x);     return 0;}// This code is contributed by Sadik Ali
// Java program to find if// there is a subtree with// given sumimport java.io.*; // Node class to create new nodeclass Node{    int data;    Node left;    Node right;    Node(int data)    {        this.data = data;    }} class GFG{    static int count = 0;    static Node ptr;         // Utility function to count subtrees that    // sum up to a given value x    int countSubtreesWithSumXUtil(Node root, int x)    {        int l = 0, r = 0;        if(root == null) return 0;        l += countSubtreesWithSumXUtil(root.left, x);        r += countSubtreesWithSumXUtil(root.right, x);        if(l + r + root.data == x) count++;        if(ptr != root) return l + root.data + r;        return count;    }     // Driver Code    public static void main(String[] args)    {        /* binary tree creation            5        / \        -10 3        / \ / \        9 8 -4 7        */        Node root = new Node(5);        root.left = new Node(-10);        root.right = new Node(3);        root.left.left = new Node(9);        root.left.right = new Node(8);        root.right.left = new Node(-4);        root.right.right = new Node(7);        int x = 7;        ptr = root; // assigning global value of ptr        System.out.println("Count = " +               new GFG().countSubtreesWithSumXUtil(root, x));    }} // This code is submitted by Devarshi_Singh
# Python3 program to find if there is# a subtree with given sum  # Structure of a node of binary treeclass Node:         def __init__(self, data):                 self.data = data        self.left = None        self.right = None  # Function to get a new nodedef getNode(data):     # Allocate space    newNode = Node(data)    return newNode count = 0ptr = None # Utility function to count subtrees that# sum up to a given value xdef countSubtreesWithSumXUtil(root, x):         global count, ptr     l = 0    r = 0         if (root == None):        return 0            l += countSubtreesWithSumXUtil(root.left, x)    r += countSubtreesWithSumXUtil(root.right, x)      if (l + r + root.data == x):        count += 1      if (ptr != root):        return l + root.data + r          return count      # Driver codeif __name__=='__main__':         ''' binary tree creation              5            /  \          -10   3          / \  / \          9 8 -4 7    '''         root = getNode(5)    root.left = getNode(-10)    root.right = getNode(3)    root.left.left = getNode(9)    root.left.right = getNode(8)    root.right.left = getNode(-4)    root.right.right = getNode(7)      x = 7    ptr = root         print("Count = " + str(countSubtreesWithSumXUtil(        root, x)))  # This code is contributed by pratham76
// C# program to find if// there is a subtree with// given sumusing System;  // Node class to// create new nodepublic class Node{  public int data;  public Node left;  public Node right;  public Node(int data)  {    this.data = data;  }}  class GFG{ static int count = 0;static Node ptr; // Utility function to count subtrees// that sum up to a given value xint countSubtreesWithSumXUtil(Node root,                              int x){  int l = 0, r = 0;  if(root == null) return 0;  l += countSubtreesWithSumXUtil(root.left, x);  r += countSubtreesWithSumXUtil(root.right, x);  if(l + r + root.data == x) count++;  if(ptr != root) return l + root.data + r;  return count;} // Driver Codepublic static void Main(string[] args){  /* binary tree creation            5        / \        -10 3        / \ / \        9 8 -4 7        */  Node root = new Node(5);  root.left = new Node(-10);  root.right = new Node(3);  root.left.left = new Node(9);  root.left.right = new Node(8);  root.right.left = new Node(-4);  root.right.right = new Node(7);  int x = 7;     // Assigning global value of ptr  ptr = root;  Console.Write("Count = " +          new GFG().countSubtreesWithSumXUtil(root, x));}} // This code is contributed by rutvik_56
<script>     // JavaScript program to find if    // there is a subtree with    // given sum         class Node    {        constructor(data) {           this.left = null;           this.right = null;           this.data = data;        }    }         let count = 0;    let ptr;          // Utility function to count subtrees that    // sum up to a given value x    function countSubtreesWithSumXUtil(root, x)    {        let l = 0, r = 0;        if(root == null) return 0;        l += countSubtreesWithSumXUtil(root.left, x);        r += countSubtreesWithSumXUtil(root.right, x);        if(l + r + root.data == x) count++;        if(ptr != root) return l + root.data + r;        return count;    }         /* binary tree creation           5          / \          -10 3          / \ / \          9 8 -4 7          */    let root = new Node(5);    root.left = new Node(-10);    root.right = new Node(3);    root.left.left = new Node(9);    root.left.right = new Node(8);    root.right.left = new Node(-4);    root.right.right = new Node(7);    let x = 7;    ptr = root; // assigning global value of ptr    document.write("Count = " + countSubtreesWithSumXUtil(root, x));   </script>
Output: 
Count = 2
Time Complexity: O(n).
andrew1234
shrikanth13
PranchalKatiyar
chsadik99
Devarshi_Singh
rutvik_56
pratham76
mukesh07
decode2207
arorakashish0911
Microsoft
Recursion
Tree
Microsoft
Recursion
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Print all possible combinations of r elements in a given array of size n
Backtracking | Introduction
Recursive Practice Problems with Solutions
Write a program to reverse digits of a number
Recursive Functions
Tree Traversals (Inorder, Preorder and Postorder)
Binary Tree | Set 1 (Introduction)
Level Order Binary Tree Traversal
AVL Tree | Set 1 (Insertion)
Inorder Tree Traversal without Recursion