Given an Acyclic Undirected Graph in the form of a Binary Tree with root at vertex 1 and values at each vertices [1, N] denoted by the array arr[], the task is to find number of root to leaf paths that contain atmost m consecutive nodes with value K.
Example:
Input: arr[] = {1, 0, 1, 0, 0, 1, 0}, K = 1, M = 2
Output: 3
Explanation:
Path 1 : 1 -> 2 -> 4 contains maximum 1 consecutive K
Path 2 : 1 -> 2 -> 5 contains maximum 1 consecutive K
Path 3 : 1 -> 3 -> 6 contains maximum 3 consecutive K
Path 4 : 1 -> 3 -> 7 contains maximum 2 consecutive K
Since the given value of M is 2, therefore there are 3 paths that contains atmost 2 consecutive K.
Input: arr[] = {2, 1, 3, 2, 1, 2, 1, 4, 3, 5, 2}, K = 2, M = 2
2 / \ 1 3 / \ / \ 2 1 2 1 / \ / \ 4 3 5 2Output: 3
Approach:
The problem can be solved using Depth First Search approach:
- Depth First Search can be used to traverse all the paths from the root vertex.
- Every time if value at the present node is K, increment the count.
- Otherwise, set count to 0.
- If count exceeds M, then return.
- Otherwise, traverse its neighboring nodes and repeat the above steps.
- Finally print the value of count obtained.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Initialize the adjacency // list and visited array vector<int> adj[100005]; int visited[100005] = { 0 }; int ans = 0; // Function to find the number of root to // leaf paths that contain atmost m // consecutive nodes with value k void dfs(int node, int count, int m, int arr[], int k) { // Mark the current node // as visited visited[node] = 1; // If value at current node is k if (arr[node - 1] == k) { // Increment counter count++; } else { count = 0; } // If count is greater than m // return from that path if (count > m) { return; } // Path is allowed if size of present node // becomes 0 i.e it has no child root and // no more than m consecutive 1's if (adj[node].size() == 1 && node != 1) { ans++; } for (auto x : adj[node]) { if (!visited[x]) { dfs(x, count, m, arr, k); } } } // Driver Code int main() { int arr[] = { 2, 1, 3, 2, 1, 2, 1 }; int N = 7, K = 2, M = 2; // Desigining the tree adj[1].push_back(2); adj[2].push_back(1); adj[1].push_back(3); adj[3].push_back(1); adj[2].push_back(4); adj[4].push_back(2); adj[2].push_back(5); adj[5].push_back(2); adj[3].push_back(6); adj[6].push_back(3); adj[3].push_back(7); adj[7].push_back(3); // Counter counts no. // of consecutive nodes int counter = 0; dfs(1, counter, M, arr, K); cout << ans << "\n"; return 0; } |
Java
// Java program to implement // the above approach import java.util.*; class GFG{ // Initialize the adjacency // list and visited array @SuppressWarnings("unchecked") static Vector<Integer> []adj = new Vector[100005]; static int []visited = new int[100005]; static int ans = 0; // Function to find the number of root to // leaf paths that contain atmost m // consecutive nodes with value k static void dfs(int node, int count, int m, int arr[], int k) { // Mark the current node // as visited visited[node] = 1; // If value at current node is k if (arr[node - 1] == k) { // Increment counter count++; } else { count = 0; } // If count is greater than m // return from that path if (count > m) { return; } // Path is allowed if size of present node // becomes 0 i.e it has no child root and // no more than m consecutive 1's if (adj[node].size() == 1 && node != 1) { ans++; } for(int x : adj[node]) { if (visited[x] == 0) { dfs(x, count, m, arr, k); } } } // Driver Code public static void main(String[] args) { int arr[] = { 2, 1, 3, 2, 1, 2, 1 }; int N = 7, K = 2, M = 2; for(int i = 0; i < adj.length; i++) adj[i] = new Vector<Integer>(); // Desigining the tree adj[1].add(2); adj[2].add(1); adj[1].add(3); adj[3].add(1); adj[2].add(4); adj[4].add(2); adj[2].add(5); adj[5].add(2); adj[3].add(6); adj[6].add(3); adj[3].add(7); adj[7].add(3); // Counter counts no. // of consecutive nodes int counter = 0; dfs(1, counter, M, arr, K); System.out.print(ans + "\n"); } } // This code is contributed by 29AjayKumar |
C#
// C# program to implement // the above approach using System; using System.Collections.Generic; class GFG{ // Initialize the adjacency // list and visited array static List<int> []adj = new List<int>[100005]; static int []visited = new int[100005]; static int ans = 0; // Function to find the number of root to // leaf paths that contain atmost m // consecutive nodes with value k static void dfs(int node, int count, int m, int []arr, int k) { // Mark the current node // as visited visited[node] = 1; // If value at current node is k if (arr[node - 1] == k) { // Increment counter count++; } else { count = 0; } // If count is greater than m // return from that path if (count > m) { return; } // Path is allowed if size of present node // becomes 0 i.e it has no child root and // no more than m consecutive 1's if (adj[node].Count == 1 && node != 1) { ans++; } foreach(int x in adj[node]) { if (visited[x] == 0) { dfs(x, count, m, arr, k); } } } // Driver Code public static void Main(String[] args) { int []arr = { 2, 1, 3, 2, 1, 2, 1 }; int K = 2, M = 2; for(int i = 0; i < adj.Length; i++) adj[i] = new List<int>(); // Desigining the tree adj[1].Add(2); adj[2].Add(1); adj[1].Add(3); adj[3].Add(1); adj[2].Add(4); adj[4].Add(2); adj[2].Add(5); adj[5].Add(2); adj[3].Add(6); adj[6].Add(3); adj[3].Add(7); adj[7].Add(3); // Counter counts no. // of consecutive nodes int counter = 0; dfs(1, counter, M, arr, K); Console.Write(ans + "\n"); } } // This code is contributed by Amit Katiyar |
4
Time Complexity: O(V + E)
Auxiliary Space: O(V)
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.
Recommended Posts:
- Count the number of paths from root to leaf of a Binary tree with given XOR value
- Root to leaf paths having equal lengths in a Binary Tree
- Remove nodes on root to leaf paths of length < K
- Count of root to leaf paths whose permutation is palindrome in a Binary Tree
- Count of root to leaf paths in a Binary Tree that form an AP
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Given a binary tree, print all root-to-leaf paths
- Sum of all the numbers that are formed from root to leaf paths
- Print root to leaf paths without using recursion
- Print all root to leaf paths with there relative positions
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Print the path common to the two paths from the root to the two given nodes
- Sum of nodes on the longest path from root to leaf node
- Root to leaf path with maximum distinct nodes
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Find maximum GCD value from root to leaf in a Binary tree
- Count of subtrees from an N-ary tree consisting of single colored nodes
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
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.


