Count of Root to Leaf Paths Consisting of at most M Consecutive Nodes having Value K


Introduction

Binary trees are fascinating data structures that have numerous applications in computer science and programming. One interesting problem is finding the count from the given tree composed of a parent along with its child nodes. The Binary tree is composed of nodes and the root node is decided and from which the child nodes can be given according the user need. The K value is decided and the way it traverses is chosen by the M value.

Count of Root to Leaf Paths

The graph is created with various nodes holding the values in form of integers. This article mainly focuses on finding the count from the starting or the root to the leaf or child node.

Example

The graph is created from the binary tree with various nodes.

  • In the above binary tree, the root node is selected as ‘8’.

  • Then two nodes are created one with a value of 3 and the other with a value of 10 occupying the left and right position of the root node.

  • Taking the node with a value of 2 as root another child is created with a value of 2 and 1 as left and right respectively.

  • And finally the child node with a value of 1 creates a child node of value -4.

Approach 1: C++ code to count of root to leaf paths consisting of at most M consecutive nodes having value K using the recursion function

To solve this problem efficiently, we will utilize basic concepts such as tree traversal algorithms and recursion.

Algorithm

Step 1: Create a structure for representing tree nodes that includes two pointers (left child and right child) along with an integer field for storing node values.

Step 2: Design a recursive function that traverses the binary tree starting from its root while maintaining track of current path length (initialized as 0), consecutive occurrences count (set as 0 initially), target value K, maximum allowed consecutive occurrences M.

Step 3: Recursively calls the function on each left and right subtree, passing updated parameters such as incremented path length and consecutive occurrence count if appropriate.

Step 4: For every visited non-null node during traversal:

a) If its value equals K, increment both variables by one.

b) If its value does not match K or exceeds M consecutive occurrences already encountered in the path so far, reset the variable back to zero.

Step 5: While traversing through the tree and if a child node has a value of zero in both cases of left and right - we can deal in two ways namely

a) Check whether the variable does not exceed M.

b) If so, increment the total count of paths from starting to the end, satisfying the condition by one.

Example

//including the all in one header
#include<bits/stdc++.h>
using namespace std;
//creating structure with two pointers as up and down
struct Vertex {
   int data;
   struct Vertex* up;
   struct Vertex* down;
};
//countPaths function declared with five arguments ; with root = end; Node= vertex; left = up; right = down
int countPaths(Vertex* end, int K, int M, int currCount, int 
consCount) {
//To check the condition when the root is equal to 1 and greater than the maximum value, the values is incremented
   if (end == NULL || consCount > M) {
      return 0;
   }
//To check when the root is equal to the K value, increment by 1
   if (end->data == K) {
      currCount++;
      consCount++;
   } else {
//If it is not equal, it will return 0
      currCount = 0;
   }
   if (end->up == NULL && end->down == NULL) {
      if (currCount <= M) {
         return 1;
      } else {
         return 0;
      }
   }
   return countPaths(end->up, K, M, currCount, consCount) + countPaths(end->down, K, M, currCount, consCount);
}
//Main function to test the implementation
int main() {
   Vertex* end = new Vertex();
   end->data = 8;
   end->up = new Vertex();
   end->up->data = 3;
   end->down = new Vertex();
   end->down->data = 10;
   end->up->up = new Vertex();
   end->up->up->data = 2;
   end->up->down = new Vertex();
   end->up->down->data = 1;
   end->up->down->up = new Vertex();
   end->up->down->up->data = -4;

   int K = 1; // Value of node
   int M = 2; // Maximum consecutive nodes
   int currCount = -1; // Current count
   int consCount = -1; // Consecutive count

   cout << "The number of paths obtained from the given graph of" << M << "nodes with a value of " << K << " is " << countPaths(end, K, M, currCount, consCount) << endl;

   return 0;
} 

Output

The number of paths obtained from the given graph of 2 nodes with a value of 1 is 3

Conclusion

In this article, we explored the problem of counting the number of paths starting from the top that is leaf to the end or root. By employing tree traversal algorithms and recursive techniques in C++, such problems can be efficiently tackled. The process of traversing through the binary tree seems difficult and with the example it is made simple.

Updated on: 09-Aug-2023

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements