Count of nodes that are greater than Ancestors
Last Updated :
23 Jan, 2023
Given the root of a tree, the task is to find the count of nodes which are greater than all of its ancestors.
Examples:
Input:
4
/ \
5 2
/ \
3 6
Output: 3
The nodes are 4, 5 and 6.
Input:
10
/ \
8 6
\ \
3 5
/
1
Output: 1
Approach: The problem can be solved using dfs. In every function call, pass a variable maxx which stores the maximum among all the nodes traversed so far, and every node whose value is greater than maxx is the node that satisfies the given condition. Hence, increment the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Tree {
int val;
Tree* left;
Tree* right;
Tree( int _val)
{
val = _val;
left = NULL;
right = NULL;
}
};
void dfs(Tree* node, int maxx, int & count)
{
if (node == NULL) {
return ;
}
else {
if (node->val > maxx)
count++;
dfs(node->left, max(maxx, node->val), count);
dfs(node->right, max(maxx, node->val), count);
}
}
int main()
{
Tree* root = new Tree(4);
root->left = new Tree(5);
root->right = new Tree(2);
root->right->left = new Tree(3);
root->right->right = new Tree(6);
int count = 0;
dfs(root, INT_MIN, count);
cout << count;
return 0;
}
|
Java
class GFG
{
static int count;
static class Tree
{
int val;
Tree left;
Tree right;
Tree( int _val)
{
val = _val;
left = null ;
right = null ;
}
};
static void dfs(Tree node, int maxx)
{
if (node == null )
{
return ;
}
else
{
if (node.val > maxx)
count++;
dfs(node.left, Math.max(maxx, node.val));
dfs(node.right, Math.max(maxx, node.val));
}
}
public static void main(String[] args)
{
Tree root = new Tree( 4 );
root.left = new Tree( 5 );
root.right = new Tree( 2 );
root.right.left = new Tree( 3 );
root.right.right = new Tree( 6 );
count = 0 ;
dfs(root, Integer.MIN_VALUE);
System.out.print(count);
}
}
|
Python3
from collections import deque
class Tree:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
count = 0
def dfs(node, maxx):
global count
if (node = = None ):
return
else :
if (node.val > maxx):
count + = 1
dfs(node.left,
max (maxx,
node.val))
dfs(node.right,
max (maxx,
node.val))
if __name__ = = '__main__' :
root = Tree( 4 )
root.left = Tree( 5 )
root.right = Tree( 2 )
root.right.left = Tree( 3 )
root.right.right = Tree( 6 )
count = 0
dfs(root,
- 10 * * 9 )
print (count)
|
C#
using System;
class GFG
{
static int count;
public class Tree
{
public int val;
public Tree left;
public Tree right;
public Tree( int _val)
{
val = _val;
left = null ;
right = null ;
}
};
static void dfs(Tree node, int maxx)
{
if (node == null )
{
return ;
}
else
{
if (node.val > maxx)
count++;
dfs(node.left, Math.Max(maxx, node.val));
dfs(node.right, Math.Max(maxx, node.val));
}
}
public static void Main(String[] args)
{
Tree root = new Tree(4);
root.left = new Tree(5);
root.right = new Tree(2);
root.right.left = new Tree(3);
root.right.right = new Tree(6);
count = 0;
dfs(root, int .MinValue);
Console.Write(count);
}
}
|
Javascript
<script>
let count=0;
class Tree
{
constructor(val)
{
this .val=val;
this .left= this .right= null ;
}
}
function dfs(node,maxx)
{
if (node == null )
{
return ;
}
else
{
if (node.val > maxx)
count++;
dfs(node.left, Math.max(maxx, node.val));
dfs(node.right, Math.max(maxx, node.val));
}
}
let root = new Tree(4);
root.left = new Tree(5);
root.right = new Tree(2);
root.right.left = new Tree(3);
root.right.right = new Tree(6);
count = 0;
dfs(root, Number.MIN_VALUE);
document.write(count);
</script>
|
Time Complexity: O(N) where N is the number of nodes in given binary tree.
Space Complexity: O(h) where h is the height of binary tree due to stack recursion call.
Share your thoughts in the comments
Please Login to comment...