Count nodes having highest value in the path from root to itself in a Binary Tree
Last Updated :
22 Jun, 2021
Given a Binary Tree, the task is to count the number of nodes in the Binary Tree, which are the highest valued node in the path from the root up to that node.
Examples:
Input: Below is the given Tree:
3
/ \
2 5
/ \
4 6
Output: 4
Explanation:
Root node satisfies the required condition.
Node 5 is the highest valued node in the path (3 -> 5).
Node 6 is the highest valued node in the path (3 -> 5 -> 6).
Node 4 is the highest valued node in the path (3 -> 2 -> 4).
Therefore, there are a total of 4 such nodes in the given binary tree.
Input: Below is the given Tree:
3
/ \
1 2
/ \
4 6
Output: 3
Approach: The idea is to perform Inorder Traversal of the given Binary Tree and update the maximum valued node obtained so far in the path after each recursive call. Follow the steps below:
- Perform Inorder Traversal on the given Binary Tree
- After each recursive call, update the maximum valued node encountered till now in the path from the root node to the current node.
- If the value of the node exceeds the maximum valued node in the path so far, increase the count by 1 and update the maximum value obtained so far.
- Proceed to the subtrees of the current node.
- After complete traversal of the Binary Tree, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int ct = 0;
struct Node
{
int val;
Node *left, *right;
Node( int x)
{
val = x;
left = right = NULL;
}
};
void find(Node *root, int mx)
{
if (root == NULL)
return ;
if (root->val >= mx)
ct++;
find(root->left, max(mx, root->val));
find(root->right, max(mx, root->val));
}
int NodesMaxInPath(Node* root)
{
find(root, INT_MIN);
return ct;
}
int main()
{
Node* root = new Node(3);
root->left = new Node(2);
root->right = new Node(5);
root->left->left = new Node(4);
root->right->right = new Node(7);
int answer = NodesMaxInPath(root);
cout << (answer);
return 0;
}
|
Java
import java.util.*;
class GfG {
static int count = 0 ;
static class Node {
int val;
Node left, right;
}
static void find(Node root, int max)
{
if (root == null )
return ;
if (root.val >= max)
count++;
find(root.left,
Math.max(max, root.val));
find(root.right,
Math.max(max, root.val));
}
static int NodesMaxInPath(Node root)
{
find(root, Integer.MIN_VALUE);
return count;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main(String[] args)
{
Node root = null ;
root = newNode( 3 );
root.left = newNode( 2 );
root.right = newNode( 5 );
root.left.left = newNode( 4 );
root.right.right = newNode( 7 );
int answer = NodesMaxInPath(root);
System.out.println(answer);
}
}
|
Python3
import sys
ct = 0
class newNode:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
def find(root, mx):
global ct
if (root = = None ):
return
if (root.val > = mx):
ct + = 1
find(root.left,
max (mx, root.val))
find(root.right,
max (mx, root.val))
def NodesMaxInPath(root):
global ct
find(root,
- sys.maxsize - 1 )
return ct
if __name__ = = '__main__' :
root = newNode( 3 )
root.left = newNode( 2 )
root.right = newNode( 5 )
root.left.left = newNode( 4 )
root.right.right = newNode( 7 )
answer = NodesMaxInPath(root)
print (answer)
|
C#
using System;
class GfG{
static int count = 0;
public class Node
{
public int val;
public Node left,
right;
}
static void find(Node root,
int max)
{
if (root == null )
return ;
if (root.val >= max)
count++;
find(root.left,
Math.Max(max, root.val));
find(root.right,
Math.Max(max, root.val));
}
static int NodesMaxInPath(Node root)
{
find(root, int .MinValue);
return count;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void Main(String[] args)
{
Node root = null ;
root = newNode(3);
root.left = newNode(2);
root.right = newNode(5);
root.left.left = newNode(4);
root.right.right = newNode(7);
int answer = NodesMaxInPath(root);
Console.WriteLine(answer);
}
}
|
Javascript
<script>
let count = 0;
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .val = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
function find(root, max)
{
if (root == null )
return ;
if (root.val >= max)
count++;
find(root.left, Math.max(max, root.val));
find(root.right, Math.max(max, root.val));
}
function NodesMaxInPath(root)
{
find(root, Number.MIN_VALUE);
return count;
}
let root = null ;
root = newNode(3);
root.left = newNode(2);
root.right = newNode(5);
root.left.left = newNode(4);
root.right.right = newNode(7);
let answer = NodesMaxInPath(root);
document.write(answer);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...