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
- Number of Paths of Weight W in a K-ary tree
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Unique paths covering every non-obstacle block exactly once in a grid
- Number of nodes greater than a given value in n-ary tree
- Print the path common to the two paths from the root to the two given nodes
- Sum of lengths of all paths possible in a given tree
- Print all k-sum paths in a binary tree
- Count all k-sum paths in a Binary Tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Count even paths in Binary Tree
- Print all the paths from root, with a specified sum in Binary tree
- Maximum product of two non-intersecting paths in a tree
- Print Palindromic Paths of Binary tree
- Count of Fibonacci paths in a Binary tree
- Count of exponential paths in a Binary Tree
- Given a binary tree, print all root-to-leaf paths
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Count of root to leaf paths in a Binary Tree that form an AP