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,
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:
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.
- Count Non-Leaf nodes in a Binary Tree
- Count nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Program to count leaf nodes in a binary tree
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Iterative program to count leaf nodes in a Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count of nodes in a Binary Tree whose child is its prime factors
- Count full nodes in a Binary tree (Iterative and Recursive)
- Least Common Ancestor of any number of nodes in Binary Tree
- Relationship between number of nodes and height of 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
- Count nodes in the given tree whose weight is a fibonacci number
- Count the nodes in the given Tree whose weight is a Perfect Number
- Count the nodes in the given tree whose weight is a powerful number
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Count of all prime weight nodes between given nodes in the given 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 firstname.lastname@example.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