Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Stack
  • Practice Stack
  • MCQs on Stack
  • Stack Tutorial
  • Stack Operations
  • Stack Implementations
  • Monotonic Stack
  • Infix to Postfix
  • Prefix to Postfix
  • Prefix to Infix
  • Advantages & Disadvantages
Open In App
Next Article:
Count of nodes in a Binary tree with immediate children as its factors
Next article icon

Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X

Last Updated : 22 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an integer X and a binary tree, the task is to count the number of triplet triplets of nodes such that their sum is greater than X and they have a grandparent -> parent -> child relationship.
 

Example:
 

Input: X = 100                 10          /           \        1              22     /    \          /    \   35      4       15       67  /  \    /  \    /  \     /  \ 57  38  9   10  110 312  131 414                     /          \                    8            39 Output: 6 The triplets are: 22 -> 15 -> 110 22 -> 15 -> 312 15 -> 312 -> 8 22 -> 67 -> 131 22 -> 67 -> 414 67 -> 414 -> 39

Approach: The problem can be solved using a rolling sum up approach with a rolling period as 3 (grandparent -> parent -> child) 

  1. Traverse the tree in preorder or postorder (INORDER WON’T WORK)
  2. Maintain a stack where we maintain rolling sum with a rolling period as 3
  3. Whenever we have more than 3 elements in the stack and if the topmost value is greater than X, we increment the result by 1.
  4. When we move up the recursion tree we do a POP operation on the stack so that all the rolling sums of lower levels get removed from the stack.

Below is the implementation of the above approach:
 

C++




#include <iostream>
#include <vector>
 
using namespace std;
 
// Class to store node information
class Node {
public:
    int val;
    Node* left;
    Node* right;
 
    Node(int value = 0, Node* l = nullptr,
         Node* r = nullptr)
        : val(value)
        , left(l)
        , right(r)
    {
    }
};
 
// Stack to perform stack operations
class Stack {
public:
    vector<int> stack;
 
    int size() { return stack.size(); }
 
    int top()
    {
        return (size() > 0) ? stack[size() - 1] : 0;
    }
 
    void push(int val) { stack.push_back(val); }
 
    void pop()
    {
        if (size() >= 1) {
            stack.pop_back();
        }
        else {
            stack.clear();
        }
    }
 
    // Period is 3 to satisfy grandparent-parent-child
    // relationship
    void rolling_push(int val, int period = 3)
    {
 
        // Find the index of element to remove
        int to_remove_idx = size() - period;
 
        // If index is out of bounds then we remove nothing,
        // i.e, 0
        int to_remove = (to_remove_idx < 0)
                            ? 0
                            : stack[to_remove_idx];
 
        // For rolling sum what we want is that at each
        // index i, we remove out-of-period elements, but
        // since we are not maintaining a separate list of
        // actual elements, we can get the actual element by
        // taking diff between current and previous element.
        // So every time we remove an element, we also add
        // the element just before it, which is equivalent
        // to removing the actual value and not the rolling
        // sum.
 
        // If index is out of bounds or 0 then we add
        // nothing i.e, 0, because there is no previous
        // element
        int to_add = (to_remove_idx <= 0)
                         ? 0
                         : stack[to_remove_idx - 1];
 
        // If stack is empty then just push the value
        // Else add last element to current value to get
        // rolling sum then subtract out-of-period elements,
        // then finally add the element just before
        // out-of-period element
        if (size() <= 0) {
            push(val);
        }
        else {
            push(val + stack[size() - 1] - to_remove
                 + to_add);
        }
    }
 
    void show()
    {
        for (auto item : stack) {
            cout << item << " ";
        }
        cout << endl;
    }
};
 
// Global variables used by count_with_greater_sum()
int count = 0;
Stack s;
 
void count_with_greater_sum(Node* root_node, int x)
{
 
    if (!root_node) {
        return;
    }
 
    s.rolling_push(root_node->val);
 
    if (s.size() >= 3 && s.top() > x) {
        count++;
    }
 
    count_with_greater_sum(root_node->left, x);
    count_with_greater_sum(root_node->right, x);
 
    // Moving up the tree so pop the last element
    s.pop();
}
 
int main()
{
    Node* root = new Node(10);
 
    root->left = new Node(1);
    root->right = new Node(22);
 
    root->left->left = new Node(35);
    root->left->right = new Node(4);
 
    root->right->left = new Node(15);
    root->right->right = new Node(67);
 
    root->left->left->left = new Node(57);
    root->left->left->right = new Node(38);
 
    root->left->right->left = new Node(9);
    root->left->right->right = new Node(10);
 
    root->right->left->left = new Node(110);
    root->right->left->right = new Node(312);
 
    root->right->right->left = new Node(131);
    root->right->right->right = new Node(414);
 
    root->right->left->right->left = new Node(8);
 
    root->right->right->right->right = new Node(39);
 
    count_with_greater_sum(root, 100);
    cout << count << endl;
}
 
 

Python3




# Python3 implementation of the approach
 
# Class to store node information
class Node:
    def __init__(self, val = None, left = None, right = None):
        self.val = val
        self.left = left
        self.right = right
 
# Stack to perform stack operations
class Stack:
    def __init__(self):
        self.stack = []
 
    @property
    def size(self):
        return len(self.stack)
 
    def top(self):
        return self.stack[self.size - 1] if self.size > 0 else 0
 
    def push(self, val):
        self.stack.append(val)
 
    def pop(self):
        if self.size >= 1:
            self.stack.pop(self.size - 1)
        else:
            self.stack = []
 
    # Period is 3 to satisfy grandparent-parent-child relationship
    def rolling_push(self, val, period = 3):
         
        # Find the index of element to remove
        to_remove_idx = self.size - period
 
        # If index is out of bounds then we remove nothing, i.e, 0
        to_remove = 0 if to_remove_idx < 0 else self.stack[to_remove_idx]
 
        # For rolling sum what we want is that at each index i,
        # we remove out-of-period elements, but since we are not
        # maintaining a separate list of actual elements,
        # we can get the actual element by taking diff between current
        # and previous element. So every time we remove an element,
        # we also add the element just before it, which is
        # equivalent to removing the actual value and not the rolling sum.
 
        # If index is out of bounds or 0 then we add nothing
        # i.e, 0, because there is no previous element
        to_add = 0 if to_remove_idx <= 0 else self.stack[to_remove_idx - 1]
 
        # If stack is empty then just push the value
        # Else add last element to current value to get rolling sum
        # then subtract out-of-period elements,
        # then finally add the element just before out-of-period element
        self.push(val if self.size <= 0 else val + self.stack[self.size - 1]
            - to_remove + to_add)
 
    def show(self):
        for item in self.stack:
            print(item)
 
 
# Global variables used by count_with_greater_sum()
count = 0
s = Stack()
 
def count_with_greater_sum(root_node, x):
    global s, count
 
    if not root_node:
        return 0
 
    s.rolling_push(root_node.val)
 
    if s.size >= 3 and s.top() > x:
        count += 1
 
    count_with_greater_sum(root_node.left, x)
    count_with_greater_sum(root_node.right, x)
 
    # Moving up the tree so pop the last element
    s.pop()
 
 
if __name__ == '__main__':
    root = Node(10)
 
    root.left = Node(1)
    root.right = Node(22)
 
    root.left.left = Node(35)
    root.left.right = Node(4)
 
    root.right.left = Node(15)
    root.right.right = Node(67)
 
    root.left.left.left = Node(57)
    root.left.left.right = Node(38)
 
    root.left.right.left = Node(9)
    root.left.right.right = Node(10)
 
    root.right.left.left = Node(110)
    root.right.left.right = Node(312)
 
    root.right.right.left = Node(131)
    root.right.right.right = Node(414)
 
    root.right.left.right.left = Node(8)
 
    root.right.right.right.right = Node(39)
 
    count_with_greater_sum(root, 100)
 
    print(count)
 
 

Javascript




// Class to store node information
class Node {
  constructor(val = null, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}
 
// Stack to perform stack operations
class Stack {
  constructor() {
    this.stack = [];
  }
 
  get size() {
    return this.stack.length;
  }
 
  top() {
    return this.size > 0 ? this.stack[this.size - 1] : 0;
  }
 
  push(val) {
    this.stack.push(val);
  }
 
  pop() {
    if (this.size >= 1) {
      this.stack.pop();
    } else {
      this.stack = [];
    }
  }
 
  // Period is 3 to satisfy grandparent-parent-child relationship
  rolling_push(val, period = 3) {
    // Find the index of element to remove
    const to_remove_idx = this.size - period;
 
    // If index is out of bounds then we remove nothing, i.e, 0
    const to_remove = to_remove_idx < 0 ? 0 : this.stack[to_remove_idx];
     
     
        // For rolling sum what we want is that at each index i,
        // we remove out-of-period elements, but since we are not
        // maintaining a separate list of actual elements,
        // we can get the actual element by taking diff between current
        // and previous element. So every time we remove an element,
        // we also add the element just before it, which is
        // equivalent to removing the actual value and not the rolling sum.
 
        // If index is out of bounds or 0 then we add nothing
        // i.e, 0, because there is no previous element
 
    const to_add = to_remove_idx <= 0 ? 0 : this.stack[to_remove_idx - 1];
     
    // If stack is empty then just push the value
    // Else add last element to current value to get rolling sum
    // then subtract out-of-period elements,
    // then finally add the element just before out-of-period element
    this.push(
      val +
        (this.size <= 0 ? 0 : this.stack[this.size - 1]) -
        to_remove +
        to_add
    );
  }
 
  show() {
    for (const item of this.stack) {
      console.log(item);
    }
  }
}
 
// Global variables used by count_with_greater_sum()
let count = 0;
const s = new Stack();
 
function count_with_greater_sum(root_node, x) {
  if (!root_node) {
    return 0;
  }
 
  s.rolling_push(root_node.val);
 
  if (s.size >= 3 && s.top() > x) {
    count += 1;
  }
 
  count_with_greater_sum(root_node.left, x);
  count_with_greater_sum(root_node.right, x);
 
  // Moving up the tree so pop the last element
  s.pop();
}
 
const root = new Node(10);
 
root.left = new Node(1);
root.right = new Node(22);
 
root.left.left = new Node(35);
root.left.right = new Node(4);
 
root.right.left = new Node(15);
root.right.right = new Node(67);
 
root.left.left.left = new Node(57);
root.left.left.right = new Node(38);
 
root.left.right.left = new Node(9);
root.left.right.right = new Node(10);
 
root.right.left.left = new Node(110);
root.right.left.right = new Node(312);
 
root.right.right.left = new Node(131);
root.right.right.right = new Node(414);
 
root.right.left.right.left = new Node(8);
 
root.right.right.right.right = new Node(39);
 
count_with_greater_sum(root, 100);
 
console.log(count);
 
// This code is contributed by Prince
 
 

Java




import java.util.*;
 
class Node {
    int val;
    Node left;
    Node right;
 
    Node(int value, Node l, Node r)
    {
        val = value;
        left = l;
        right = r;
    }
 
    Node(int value) { this(value, null, null); }
}
 
class Stack {
    ArrayList<Integer> stack = new ArrayList<>();
 
    int size() { return stack.size(); }
 
    int top()
    {
        return (size() > 0) ? stack.get(size() - 1) : 0;
    }
 
    void push(int val) { stack.add(val); }
 
    void pop()
    {
        if (size() >= 1) {
            stack.remove(size() - 1);
        }
        else {
            stack.clear();
        }
    }
 
    void rolling_push(int val, int period)
    {
        int to_remove_idx = size() - period;
        int to_remove = (to_remove_idx < 0)
                            ? 0
                            : stack.get(to_remove_idx);
 
        int to_add = (to_remove_idx <= 0)
                         ? 0
                         : stack.get(to_remove_idx - 1);
 
        if (size() <= 0) {
            push(val);
        }
        else {
            push(val + stack.get(size() - 1) - to_remove
                 + to_add);
        }
    }
 
    void show()
    {
        for (int item : stack) {
            System.out.print(item + " ");
        }
        System.out.println();
    }
}
 
public class Main {
    static int count = 0;
    static Stack s = new Stack();
 
    static void count_with_greater_sum(Node root_node,
                                       int x)
    {
        if (root_node == null) {
            return;
        }
 
        s.rolling_push(root_node.val, 3);
 
        if (s.size() >= 3 && s.top() > x) {
            count++;
        }
 
        count_with_greater_sum(root_node.left, x);
        count_with_greater_sum(root_node.right, x);
 
        s.pop();
    }
 
    public static void main(String[] args)
    {
        Node root = new Node(10);
 
        root.left = new Node(1);
        root.right = new Node(22);
 
        root.left.left = new Node(35);
        root.left.right = new Node(4);
 
        root.right.left = new Node(15);
        root.right.right = new Node(67);
 
        root.left.left.left = new Node(57);
        root.left.left.right = new Node(38);
 
        root.left.right.left = new Node(9);
        root.left.right.right = new Node(10);
 
        root.right.left.left = new Node(110);
        root.right.left.right = new Node(312);
 
        root.right.right.left = new Node(131);
        root.right.right.right = new Node(414);
 
        root.right.left.right.left = new Node(8);
 
        root.right.right.right.right = new Node(39);
 
        count_with_greater_sum(root, 100);
        System.out.println(count);
    }
}
 
 

C#




using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
// C# code addition
 
class Node {
    public int val;
    public Node left;
    public Node right;
 
    public Node(int value, Node l, Node r)
    {
        val = value;
        left = l;
        right = r;
    }
 
    public Node(int value){
        val = value;
        left = null;
        right = null;
    }
}
 
class stack {
    public List<int> st = new List<int>();
 
    public int size() { return st.Count; }
 
    public int top()
    {
        return (size() > 0) ? st[size() - 1]: 0;
    }
 
    public void push(int val) { st.Add(val); }
 
    public void pop()
    {
        if (size() >= 1) {
            st.RemoveAt(size() - 1);
        }
        else {
            st.Clear();
        }
    }
 
    public void rolling_push(int val, int period)
    {
        int to_remove_idx = size() - period;
        int to_remove = (to_remove_idx < 0)
                            ? 0
                            : st[to_remove_idx];
 
        int to_add = (to_remove_idx <= 0)
                         ? 0
                         : st[to_remove_idx - 1];
 
        if (size() <= 0) {
            push(val);
        }
        else {
            push(val + st[size() - 1]- to_remove
                 + to_add);
        }
    }
 
    public void show()
    {
        foreach(var item in st){
            Console.Write(item + " ");
        }
        Console.WriteLine();
    }
}
 
 
 
public class HelloWorld {
     
    static int count = 0;
    static stack s = new stack();
 
    static void count_with_greater_sum(Node root_node,
                                       int x)
    {
        if (root_node == null) {
            return;
        }
 
        s.rolling_push(root_node.val, 3);
 
        if (s.size() >= 3 && s.top() > x) {
            count++;
        }
 
        count_with_greater_sum(root_node.left, x);
        count_with_greater_sum(root_node.right, x);
 
        s.pop();
    }
     
    static void Main() {
        Node root = new Node(10);
 
        root.left = new Node(1);
        root.right = new Node(22);
 
        root.left.left = new Node(35);
        root.left.right = new Node(4);
 
        root.right.left = new Node(15);
        root.right.right = new Node(67);
 
        root.left.left.left = new Node(57);
        root.left.left.right = new Node(38);
 
        root.left.right.left = new Node(9);
        root.left.right.right = new Node(10);
 
        root.right.left.left = new Node(110);
        root.right.left.right = new Node(312);
 
        root.right.right.left = new Node(131);
        root.right.right.right = new Node(414);
 
        root.right.left.right.left = new Node(8);
 
        root.right.right.right.right = new Node(39);
 
        count_with_greater_sum(root, 100);
        Console.WriteLine(count);
    }
}
 
// The code is contributed by Arushi Jindal.
 
 
Output:
6

 

Efficient Approach: The problem can be solved by maintaining 3 variables called grandparent, parent, and child. It can be done in constant space without using other data structures. 

  1. Traverse the tree in preorder
  2. Maintain 3 variables called grandParent, parent, and child
  3. Whenever we have sum more than the target we can increase the count or print the triplet.

Below is the implementation of the above approach: 

C++




// CPP implementation to print
// the nodes having a single child
#include <bits/stdc++.h>
using namespace std;
 
// Class of the Binary Tree node
struct Node
{
  int data;
  Node *left, *right;
 
  Node(int x)
  {
    data = x;
    left = right = NULL;
  }
};
 
// global variable
int count = 0;
 
void preorder(Node* grandParent,
              Node* parent,
              Node* child,
              int sum)
{
  if(grandParent != NULL &&
     parent != NULL &&
     child != NULL &&
     (grandParent -> data +
      parent -> data +
      child->data) > sum)
  {
    count++;
    //uncomment below lines if you
    // want to print triplets
    /*System->out->print(grandParent
      ->data+"-->"+parent->data+"-->
      "+child->data);
      System->out->println();*/
  }
  if(child == NULL)
    return;
   
  preorder(parent, child,
           child -> left, sum);
  preorder(parent, child,
           child -> right, sum);
}
 
//Driver code
int main()
{
  Node *r10 = new Node(10);
  Node *r1 = new Node(1);
  Node *r22 = new Node(22);
  Node *r35 = new Node(35);
  Node *r4 = new Node(4);
  Node *r15 = new Node(15);
  Node *r67 = new Node(67);
  Node *r57 = new Node(57);
  Node *r38 = new Node(38);
  Node *r9 = new Node(9);
  Node *r10_2 = new Node(10);
  Node *r110 = new Node(110);
  Node *r312 = new Node(312);
  Node *r131 = new Node(131);
  Node *r414 = new Node(414);
  Node *r8 = new Node(8);
  Node *r39 = new Node(39);
 
  r10 -> left = r1;
  r10 -> right = r22;
  r1 -> left = r35;
  r1 -> right = r4;
  r22 -> left = r15;
  r22 -> right = r67;
  r35 -> left = r57;
  r35 -> right = r38;
  r4 -> left = r9;
  r4 -> right = r10_2;
  r15 -> left = r110;
  r15 -> right = r312;
  r67 -> left = r131;
  r67 -> right = r414;
  r312 -> left = r8;
  r414 -> right = r39;
 
  preorder(NULL, NULL,
           r10, 100);
  cout << cont;
}
 
// This code is contributed by Mohit Kumar 29
 
 

Java




class Node{
    int data;
    Node left;
    Node right;
    public Node(int data)
    {
        this.data=data;
    }
}
 class TreeTriplet {
    static int count=0; // global variable
    public void preorder(Node grandParent,Node parent,Node child,int sum)
    {
        if(grandParent!=null && parent!=null && child!=null && (grandParent.data+parent.data+child.data) > sum)
        {
            count++;
            //uncomment below lines if you want to print triplets
            /*System.out.print(grandParent.data+"-->"+parent.data+"-->"+child.data);
            System.out.println();*/           
        }
        if(child==null)
          return;       
        preorder(parent,child,child.left,sum);
        preorder(parent,child,child.right,sum);       
    }
    public static void main(String args[])
    {       
        Node r10 = new Node(10);
        Node r1 = new Node(1);
        Node r22 = new Node(22);
        Node r35 = new Node(35);
        Node r4 = new Node(4);
        Node r15 = new Node(15);
        Node r67 = new Node(67);
        Node r57 = new Node(57);
        Node r38 = new Node(38);
        Node r9 = new Node(9);
        Node r10_2 = new Node(10);
        Node r110 = new Node(110);
        Node r312 = new Node(312);
        Node r131 = new Node(131);
        Node r414 = new Node(414);
        Node r8 = new Node(8);
        Node r39 = new Node(39);
         
        r10.left=r1;
        r10.right=r22;
        r1.left=r35;
        r1.right=r4;
        r22.left=r15;
        r22.right=r67;
        r35.left=r57;
        r35.right=r38;
        r4.left=r9;
        r4.right=r10_2;
        r15.left=r110;
        r15.right=r312;
        r67.left=r131;
        r67.right=r414;
        r312.left=r8;
        r414.right=r39;   
 
        TreeTriplet p = new TreeTriplet();
        p.preorder(null, null, r10,100);
        System.out.println(count);
}
     
     
}
 // This code is contributed by Akshay Siddhpura
 
 

Python3




# Python3 program to implement
# the above approach
class Node:
     
    def __init__(self,
                 data):
         
        self.left = None
        self.right = None
        self.data = data       
 
# global variable
count = 0
     
def preorder(grandParent, parent,
             child, sum):
     
    global count
         
    if(grandParent != None and
       parent != None and
       child != None and
       (grandParent.data +
        parent.data +
        child.data) > sum):    
        count += 1
             
        # uncomment below lines if
        # you want to print triplets 
        # System.out.print(grandParent.
        # data+"-->"+parent.data+"-->
        # "+child.data); System.out.println();          
    if(child == None):
        return;
           
    preorder(parent, child,
             child.left, sum);
    preorder(parent, child,
             child.right, sum); 
     
# Driver code
if __name__ == "__main__":
     
    r10 = Node(10);
    r1 = Node(1);
    r22 = Node(22);
    r35 = Node(35);
    r4 = Node(4);
    r15 = Node(15);
    r67 = Node(67);
    r57 = Node(57);
    r38 = Node(38);
    r9 = Node(9);
    r10_2 = Node(10);
    r110 = Node(110);
    r312 = Node(312);
    r131 = Node(131);
    r414 = Node(414);
    r8 = Node(8);
    r39 = Node(39);
           
    r10.left = r1;
    r10.right = r22;
    r1.left = r35;
    r1.right = r4;
    r22.left = r15;
    r22.right = r67;
    r35.left = r57;
    r35.right = r38;
    r4.left = r9;
    r4.right = r10_2;
    r15.left = r110;
    r15.right = r312;
    r67.left = r131;
    r67.right = r414;
    r312.left = r8;
    r414.right = r39;    
   
    preorder(None, None,
             r10, 100)
    print(count);
         
# This code is contributed by Rutvik_56
 
 

C#




// C# program to find an index which has
// same number of even elements on left and
// right, Or same number of odd elements on
// left and right.
using System;
     
public class Node
{
    public int data;
    public Node left;
    public Node right;
    public Node(int data)
    {
        this.data = data;
    }
}
 
class GFG
{
    static int count = 0; // global variable
    public void preorder(Node grandParent,
                         Node parent,
                         Node child, int sum)
    {
        if(grandParent != null && parent != null &&
                 child != null && (grandParent.data +
                     parent.data + child.data) > sum)
        {
            count++;
             
            // uncomment below lines if you want to print triplets
            /*System.out.print(grandParent.data+"-->"+parent.data+"-->"+child.data);
            System.out.println();*/       
        }
        if(child == null)
            return;    
        preorder(parent,child,child.left,sum);
        preorder(parent,child,child.right,sum);    
    }
     
    // Driver Code
    public static void Main(String []args)
    {    
        Node r10 = new Node(10);
        Node r1 = new Node(1);
        Node r22 = new Node(22);
        Node r35 = new Node(35);
        Node r4 = new Node(4);
        Node r15 = new Node(15);
        Node r67 = new Node(67);
        Node r57 = new Node(57);
        Node r38 = new Node(38);
        Node r9 = new Node(9);
        Node r10_2 = new Node(10);
        Node r110 = new Node(110);
        Node r312 = new Node(312);
        Node r131 = new Node(131);
        Node r414 = new Node(414);
        Node r8 = new Node(8);
        Node r39 = new Node(39);
         
        r10.left = r1;
        r10.right = r22;
        r1.left = r35;
        r1.right = r4;
        r22.left = r15;
        r22.right = r67;
        r35.left = r57;
        r35.right = r38;
        r4.left = r9;
        r4.right = r10_2;
        r15.left = r110;
        r15.right = r312;
        r67.left = r131;
        r67.right = r414;
        r312.left = r8;
        r414.right = r39;
 
        GFG p = new GFG();
        p.preorder(null, null, r10,100);
        Console.WriteLine(count);
}
}
 
// This code is contributed by 29AjayKumar
 
 

Javascript




<script>
 
class Node
{
    constructor(data)
    {
        this.data = data;
        this.left = this.right = null;
    }
}
 
let count = 0; // global variable
 
function preorder(grandParent, parent, child, sum)
{
    if(grandParent != null && parent != null && child != null && (grandParent.data+parent.data+child.data) > sum)
        {
            count++;
            // uncomment below lines if you want to print triplets
            /* System.out.print(grandParent.data+"-->"+parent.data+"-->"+child.data);
            System.out.println();*/          
        }
        if(child == null)
          return;      
        preorder(parent, child, child.left, sum);
        preorder(parent, child, child.right, sum); 
}
 
let r10 = new Node(10);
let r1 = new Node(1);
let r22 = new Node(22);
let r35 = new Node(35);
let r4 = new Node(4);
let r15 = new Node(15);
let r67 = new Node(67);
let r57 = new Node(57);
let r38 = new Node(38);
let r9 = new Node(9);
let r10_2 = new Node(10);
let r110 = new Node(110);
let r312 = new Node(312);
let r131 = new Node(131);
let r414 = new Node(414);
let r8 = new Node(8);
let r39 = new Node(39);
 
r10.left = r1;
r10.right = r22;
r1.left = r35;
r1.right = r4;
r22.left = r15;
r22.right = r67;
r35.left = r57;
r35.right = r38;
r4.left = r9;
r4.right = r10_2;
r15.left = r110;
r15.right = r312;
r67.left = r131;
r67.right = r414;
r312.left = r8;
r414.right = r39;  
 
preorder(null, null, r10,100);
document.write(count);
 
// This code is contributed by unknown2108
</script>
 
 
Output:
6

 

Time complexity: O(n) 

Auxiliary Space : The space complexity of the above code is O(1), as it does not use any additional data structures and only uses a few variables for storing intermediate values. (the recursion stack is not being considered for this approach).



Next Article
Count of nodes in a Binary tree with immediate children as its factors

V

Vijayant Soni
Improve
Article Tags :
  • Algorithms
  • Data Structures
  • DSA
  • Python
  • Recursion
  • Stack
  • Tree
Practice Tags :
  • Algorithms
  • Data Structures
  • python
  • Recursion
  • Stack
  • Tree

Similar Reads

  • Sum of all the child nodes with even grandparents in a Binary Tree
    Given a Binary Tree, calculate the sum of nodes with even valued Grandparents.Examples: Input: 22 / \ 3 8 / \ / \ 4 8 1 9 \ 2 Output: 24 Explanation The nodes 4, 8, 2, 1, 9 has even value grandparents. Hence sum = 4 + 8 + 1 + 9 + 2 = 24. Input: 1 / \ 2 3 / \ / \ 4 5 6 7 / 8 Output: 8 Explanation Onl
    6 min read
  • Count pairs in a binary tree whose sum is equal to a given value x
    Given a binary tree containing n distinct numbers and a value x. The problem is to count pairs in the given binary tree whose sum is equal to the given value x. Examples: Input : 5 / \ 3 7 / \ / \ 2 4 6 8 x = 10 Output : 3 The pairs are (3, 7), (2, 8) and (4, 6). 1) Naive Approach: One by one get ea
    15+ min read
  • Print all pairs from two BSTs whose sum is greater than the given value
    Given two Binary Search Tree (BSTs) and a value X, the problem is to print all pairs from both the BSTs whose sum is greater than the given value X.\ Examples: Input: BST 1: 5 / \ 3 7 / \ / \ 2 4 6 8 BST 2: 10 / \ 6 15 / \ / \ 3 8 11 18X = 20Output: The pairs are: (3, 18) (4, 18) (5, 18) (6, 18) (7,
    15+ min read
  • Count of nodes in a Binary tree with immediate children as its factors
    Given a Binary Tree, the task is to print the count of nodes whose immediate children are its factors. Examples: Input: 1 / \ 15 20 / \ / \ 3 5 4 2 \ / 2 3 Output: 2 Explanation: Children of 15 (3, 5) are factors of 15 Children of 20 (4, 2) are factors of 20 Input: 7 / \ 210 14 / \ \ 70 14 30 / \ /
    15 min read
  • Count of triples (A, B, C) where A*C is greater than B*B
    Given three integers A, B and C. The task is to count the number of triples (a, b, c) such that a * c > b2, where 0 < a <= A, 0 < b <= B and 0 < c <= C.Examples: Input: A = 3, B = 2, C = 2 Output: 6 Following triples are counted : (1, 1, 2), (2, 1, 1), (2, 1, 2), (3, 1, 1), (3,
    12 min read
  • Convert an arbitrary Binary Tree to a tree that holds Children Sum Property - Set 2
    Given an arbitrary binary tree, your task is to convert it to a binary tree that holds the Children Sum Property, by incrementing the data values of any node. Note: The structure of tree can't be changed and the node values can't be decremented. Also, there exist multiple possible answers. Example:
    10 min read
  • Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
    Given an arbitrary binary tree, your task is to convert it to a binary tree that holds Children Sum Property, by incrementing the data values of any node. Note: The structure of the tree can't be changed and the node values can't be decremented. Also, there exist multiple possible answers. Example:
    15+ min read
  • Given a n-ary tree, count number of nodes which have more number of children than parents
    Given a N-ary tree represented as adjacency list, we need to write a program to count all such nodes in this tree which has more number of children than its parent.For Example, In the above tree, the count will be 1 as there is only one such node which is '2' which has more number of children than i
    6 min read
  • Count the Number of Binary Search Trees present in a Binary Tree
    Given a binary tree, the task is to count the number of Binary Search Trees present in it. Examples: Input: 1 / \ 2 3 / \ / \ 4 5 6 7 Output: 4Here each leaf node represents a binary search tree and there are total 4 nodes. Input: 11 / \ 8 10 / / \ 5 9 8 / \ 4 6 Output: 6 Sub-tree rooted under node
    10 min read
  • Count nodes with two children at level L in a Binary Tree
    Given a Binary tree, the task is to count the number of nodes with two children at a given level L. Examples: Input: 1 / \ 2 3 / \ \ 4 5 6 / / \ 7 8 9 L = 2 Output: 1 Input: 20 / \ 8 22 / \ / \ 5 3 4 25 / \ / \ \ 1 10 2 14 6 L = 3 Output: 2 Approach: Initialize a variable count = 0. Recursively trav
    13 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences