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
// 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); } } |
C#
// 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 |
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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Count Non-Leaf nodes in a Binary Tree
- Count nodes with two children at level L in a Binary Tree
- Program to count leaf nodes in a binary tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Iterative program to count leaf nodes in a Binary Tree
- Count full nodes in a Binary tree (Iterative and Recursive)
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count of nodes in a Binary Tree whose child is its prime factors
- Relationship between number of nodes and height of binary tree
- Least Common Ancestor of any number of nodes in Binary Tree
- Count the number of nodes at given level in a tree using BFS.
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Print the nodes of the Binary Tree whose height is a Prime number
- Count the Number of Binary Search Trees present in a Binary Tree
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

