Given a tree as set of edges such that every node has unique value. We are also given a value k, the task is to count the unique paths in the tree such that every path has a value greater than K. A path value is said to be > K if every edge contributing in the path is connecting two nodes both of which have values > K.
Approach: The idea is to not form the tree with all the given edges. We only add an edge if it satisfies the condition of > k. In this case, a number of trees will be formed. While forming the different trees, we will only add the edge into the tree if both the node value are greater than K. After this, various number of trees will be created. Run a DFS for every node which in the end traverses the complete tree with which the node is attached and count the number of nodes in every tree. The number of unique paths for every tree which has X number of nodes is X * (X – 1) / 2.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Unique paths covering every non-obstacle block exactly once in a grid
- Check whether every node of binary tree has a value K on itself or its any immediate neighbours
- Number of nodes greater than a given value in n-ary tree
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Print the path common to the two paths from the root to the two given nodes
- Print all pairs from two BSTs whose sum is greater than the given value
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Number of Paths of Weight W in a K-ary tree
- Count number of times each Edge appears in all possible paths of a given Tree
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Maximum XOR with given value in the path from root to given node in the tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
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.