Count the number of visible nodes in Binary Tree
Last Updated :
23 Jun, 2021
Given a Binary tree, the task is to find the number of visible nodes in the given binary tree. A node is a visible node if, in the path from the root to the node N, there is no node with greater value than N’s,
Examples:
Input:
5
/ \
3 10
/ \ /
20 21 1
Output: 4
Explanation:
There are 4 visible nodes.
They are:
5: In the path 5 -> 3, 5 is the highest node value.
20: In the path 5 -> 3 -> 20, 20 is the highest node value.
21: In the path 5 -> 3 -> 21, 21 is the highest node value.
10: In the path 5 -> 10 -> 1, 10 is the highest node value.
Input:
-1
\
-2
\
-3
Output: 1
Approach: The idea is to first traverse the tree. Since we need to see the maximum value in the given path, the pre-order traversal is used to traverse the given binary tree. While traversing the tree, we need to keep the track of the maximum value of the node that we have seen so far. If the current node is greater or equal to the max value, then increment the count of the visible node and update the max value with the current node value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
struct Node* newnode( int data)
{
struct Node* node = new ( struct Node);
node->data = data;
node->left = node->right = NULL;
return (node);
}
int countNode = 0;
void preOrder(Node* node, int mx)
{
if (node == NULL)
{
return ;
}
if (node->data >= mx)
{
countNode++;
mx = max(node->data, mx);
}
preOrder(node->left, mx);
preOrder(node->right, mx);
}
int main()
{
struct Node* root = newnode(5);
root->left = newnode(3);
root->right = newnode(10);
root->left->left = newnode(20);
root->left->right = newnode(21);
root->right->left = newnode(1);
preOrder(root, INT_MIN);
cout << countNode;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
public class GFG {
Node root;
static int count;
static void preOrder(Node node, int max)
{
if (node == null ) {
return ;
}
if (node.data >= max) {
count++;
max = Math.max(node.data, max);
}
preOrder(node.left, max);
preOrder(node.right, max);
}
public static void main(String[] args)
{
GFG tree = new GFG();
tree.root = new Node( 5 );
tree.root.left = new Node( 3 );
tree.root.right = new Node( 10 );
tree.root.left.left = new Node( 20 );
tree.root.left.right = new Node( 21 );
tree.root.right.left = new Node( 1 );
count = 0 ;
preOrder(tree.root, Integer.MIN_VALUE);
System.out.println(count);
}
}
|
Python3
import sys
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
countNode = 0
def preOrder(node, mx):
global countNode
if (node = = None ):
return
if (node.data > = mx):
countNode + = 1
mx = max (node.data, mx)
preOrder(node.left, mx)
preOrder(node.right, mx)
if __name__ = = '__main__' :
root = newNode( 5 )
root.left = newNode( 3 )
root.right = newNode( 10 )
root.left.left = newNode( 20 )
root.left.right = newNode( 21 )
root.right.left = newNode( 1 )
preOrder(root, - sys.maxsize - 1 )
print (countNode)
|
C#
using System;
class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class GFG{
Node root;
static int count;
static void preOrder(Node node, int max)
{
if (node == null )
{
return ;
}
if (node.data >= max)
{
count++;
max = Math.Max(node.data, max);
}
preOrder(node.left, max);
preOrder(node.right, max);
}
static public void Main(String[] args)
{
GFG tree = new GFG();
tree.root = new Node(5);
tree.root.left = new Node(3);
tree.root.right = new Node(10);
tree.root.left.left = new Node(20);
tree.root.left.right = new Node(21);
tree.root.right.left = new Node(1);
count = 0;
preOrder(tree.root, int .MinValue);
Console.WriteLine(count);
}
}
|
Javascript
<script>
class Node
{
constructor(item) {
this .left = null ;
this .right = null ;
this .data = item;
}
}
let root;
let count;
function preOrder(node, max)
{
if (node == null ) {
return ;
}
if (node.data >= max) {
count++;
max = Math.max(node.data, max);
}
preOrder(node.left, max);
preOrder(node.right, max);
}
root = new Node(5);
root.left = new Node(3);
root.right = new Node(10);
root.left.left = new Node(20);
root.left.right = new Node(21);
root.right.left = new Node(1);
count = 0;
preOrder(root, Number.MIN_VALUE);
document.write(count);
</script>
|
Complexity Analysis:
Time Complexity: O(N) where N is a number of nodes in the Binary tree.
Auxiliary Space: O(H) where H is the height of the Binary tree.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...