Count the number of visible nodes in Binary Tree

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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to count the
// number of visible nodes in
// the binary tree
  
// Class containing the left and right
// child of current node and the
// key value
class Node {
    int data;
    Node left, right;
  
    // Constructor of the class
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
public class GFG {
    Node root;
  
    // Variable to keep the track
    // of visible nodes
    static int count;
  
    // Function to perform the preorder traversal
    // for the given tree
    static void preOrder(Node node, int max)
    {
  
        // Base case
        if (node == null) {
            return;
        }
  
        // If the current node value is greater
        // or equal to the max value,
        // then update count variable
        // and also update max variable
        if (node.data >= max) {
            count++;
            max = Math.max(node.data, max);
        }
  
        // Traverse to the left
        preOrder(node.left, max);
  
        // Traverse to the right
        preOrder(node.right, max);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        GFG tree = new GFG();
  
        /*
                5
               /  \
             3     10
            /  \   /
           20   21 1 
*/
  
        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);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count the
// number of visible nodes in
// the binary tree
using System;
  
// Class containing the left and right
// child of current node and the
// key value
class Node
{
    public int data;
    public Node left, right;
  
    // Constructor of the class
    public Node(int item)
    {
        data = item;
        left = right = null;
    }
}
  
class GFG{
      
Node root;
  
// Variable to keep the track
// of visible nodes
static int count;
  
// Function to perform the preorder
// traversal for the given tree
static void preOrder(Node node, int max)
{
  
    // Base case
    if (node == null)
    {
        return;
    }
  
    // If the current node value is greater
    // or equal to the max value,
    // then update count variable
    // and also update max variable
    if (node.data >= max)
    {
        count++;
        max = Math.Max(node.data, max);
    }
  
    // Traverse to the left
    preOrder(node.left, max);
  
    // Traverse to the right
    preOrder(node.right, max);
}
  
// Driver code
static public void Main(String[] args)
{
    GFG tree = new GFG();
  
/*
         5
        / \
      3       10
     / \   /
    20 21 1 
*/
  
    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);
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output:

4

Complexity Analysis:
Time Complexity: O(N) where N is number of nodes in Binary tree.
Auxiliary Space: O(H) where H is the height of Binary tree.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : amit143katiyar

Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.