Given a Binary Tree, the task is to count the number of nodes in the given Binary Tree such that the path from the root to that node contains node with value greater than or equal to that node.
Examples:
Input:
6
/ \
7 4
/ \ / \
3 7 1 2
Output: 5
Explanation:
Root node 6 is considered as its the only node
in the path from root to itself.
Node 4 has the minimum value in it's path 6->4.
Node 1 has the minimum value in it's path 6->4->1.
Node 2 has the minimum value in it's path 6->4->2.
Node 3 has the minimum value in it's path 6->7->3.
Input:
8
/ \
6 5
/ \ / \
6 7 3 9
Output: 5
Explanation:
Root node 8 is considered as its the only node
in the path from root to itself.
Node 6 has the minimum value in it's path 8->6.
Node 6 has the minimum value in it's path 8->6->6.
Node 5 has the minimum value in it's path 8->5.
Node 3 has the minimum value in it's path 8->5->3.
Approach: The idea is to do Preorder traversal on the given Binary Tree. Follow the steps below to solve the problem:
- Create a function to calculate the number of nodes that satisfy the given conditions.
- If the current node is NULL then return to the previous node.
- Use a variable minNodeVal to store the minimum node value along the path from the root to the current node.
- If the value of the current node is less than or equal to minNodeVal then increase the final count by 1 and update the value of minNodeVal.
- Call the function for the left and right child of the current node and repeat this process for every node to get the total count of the required nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
Node *left, *right;
};
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
}
void countReqNodes(Node* root,
int minNodeVal, int & ans)
{
if (root == NULL)
return ;
if (root->key <= minNodeVal) {
minNodeVal = root->key;
ans++;
}
countReqNodes(root->left,
minNodeVal, ans);
countReqNodes(root->right,
minNodeVal, ans);
}
int main()
{
Node* root = newNode(8);
root->left = newNode(6);
root->right = newNode(5);
root->left->left = newNode(6);
root->left->right = newNode(7);
root->right->left = newNode(3);
root->right->right = newNode(9);
int ans = 0, minNodeVal = INT_MAX;
countReqNodes(root, minNodeVal, ans);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int key;
Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return temp;
}
static int ans;
static void countReqNodes(Node root,
int minNodeVal)
{
if (root == null )
return ;
if (root.key <= minNodeVal)
{
minNodeVal = root.key;
ans++;
}
countReqNodes(root.left,
minNodeVal);
countReqNodes(root.right,
minNodeVal);
}
public static void main(String[] args)
{
Node root = newNode( 8 );
root.left = newNode( 6 );
root.right = newNode( 5 );
root.left.left = newNode( 6 );
root.left.right = newNode( 7 );
root.right.left = newNode( 3 );
root.right.right = newNode( 9 );
int minNodeVal = Integer.MAX_VALUE;
ans = 0 ;
countReqNodes(root, minNodeVal);
System.out.print(ans);
}
}
|
Python3
import sys
ans = 0
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def countReqNodes(root, minNodeVal):
global ans
if root = = None :
return
if root.key < = minNodeVal:
minNodeVal = root.key
ans + = 1
countReqNodes(root.left, minNodeVal)
countReqNodes(root.right, minNodeVal)
if __name__ = = '__main__' :
root = Node( 8 )
root.left = Node( 6 )
root.right = Node( 5 )
root.left.left = Node( 6 )
root.left.right = Node( 7 )
root.right.left = Node( 3 )
root.right.right = Node( 9 )
minNodeVal = sys.maxsize
countReqNodes(root, minNodeVal)
print (ans)
|
C#
using System;
class GFG{
public class Node
{
public int key;
public Node left, right;
};
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.left = temp.right = null ;
return temp;
}
static int ans;
static void countReqNodes(Node root,
int minNodeVal)
{
if (root == null )
return ;
if (root.key <= minNodeVal)
{
minNodeVal = root.key;
ans++;
}
countReqNodes(root.left,
minNodeVal);
countReqNodes(root.right,
minNodeVal);
}
public static void Main(String[] args)
{
Node root = newNode(8);
root.left = newNode(6);
root.right = newNode(5);
root.left.left = newNode(6);
root.left.right = newNode(7);
root.right.left = newNode(3);
root.right.right = newNode(9);
int minNodeVal = int .MaxValue;
ans = 0;
countReqNodes(root, minNodeVal);
Console.Write(ans);
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .left = null ;
this .right = null ;
this .key = key;
}
}
function newNode(key)
{
let temp = new Node(key);
return temp;
}
let ans;
function countReqNodes(root, minNodeVal)
{
if (root == null )
return ;
if (root.key <= minNodeVal)
{
minNodeVal = root.key;
ans++;
}
countReqNodes(root.left,
minNodeVal);
countReqNodes(root.right,
minNodeVal);
}
let root = newNode(8);
root.left = newNode(6);
root.right = newNode(5);
root.left.left = newNode(6);
root.left.right = newNode(7);
root.right.left = newNode(3);
root.right.right = newNode(9);
let minNodeVal = Number.MAX_VALUE;
ans = 0;
countReqNodes(root, minNodeVal);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!