Given a K-ary tree, where each node is having K children and each edge has some weight. All the edges i.e. K, that goes from a particular node to all its children have weights in ascending order 1, 2, 3, …, K. Find the number of paths having total weight as W (sum of all edge weights in the path) starting from root and containing atleast one edge of weight atleast M.
Input : W = 3, K = 3, M = 2 Output : 3 Explanation : One path can be (1 + 2), second can be (2 + 1) and third is 3. Input : W = 4, K = 3, M = 2 Output : 6
Approach: This problem can be solved using dynamic programming approach. The idea is to maintain two states, one for the current weight to be required and other one for a boolean variable which denotes that the current path has included an edge of weight atleast M or not. Iterate over all possible edge weights i.e. K and recursively solve for the weight W – i for 1 ≤ i ≤ K. If the current edge weight is more than or equal to M, set the boolean variable as 1 for the next call.
Below is the implementation of above approach.
Time Complexity: O(W * K)
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Choose maximum weight with given weight and value ratio
- Print all k-sum paths in a binary tree
- Print all the paths from root, with a specified sum in Binary tree
- Given a binary tree, print all root-to-leaf paths
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Root to leaf paths having equal lengths in a Binary Tree
- Number of paths with exactly k coins
- Number of palindromic paths in a matrix
- Count number of paths with at-most k turns
- Total number of decreasing paths in a matrix
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of different cyclic paths of length N in a tetrahedron
- Number of paths from source to destination in a directed acyclic graph
- CSS | font-weight Property
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.