K-Sum Paths

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 182:-K-Sum Paths

Finding K-sum paths in a binary tree involves identifying paths within the tree where the sum of node values along the path is equal to a given target value K. These paths may start and end at any node in the tree.

Here's a Python implementation to find K-sum paths in a binary tree:

pythonCopy code

class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def find_k_sum_paths(root, target_sum): def dfs(node, path): if node is None: return path.append(node.data) current_sum = 0 # Check for valid paths ending at the current node for i in range(len(path) - 1, -1, -1): current_sum += path[i] if current_sum == target_sum: print("Path:", path[i:]) dfs(node.left, path.copy()) dfs(node.right, path.copy()) dfs(root, []) # Example usage root = TreeNode(10) root.left = TreeNode(5) root.right = TreeNode(-3) root.left.left = TreeNode(3) root.left.right = TreeNode(2) root.right.right = TreeNode(11) root.left.left.left = TreeNode(3) root.left.left.right = TreeNode(-2) root.left.right.right = TreeNode(1) target = 8 print("K-Sum Paths with target", target, ":") find_k_sum_paths(root, target)

In this example, the TreeNode class represents nodes of the binary tree. The find_k_sum_paths function takes a root node and a target sum target_sum as inputs and recursively searches for K-sum paths using a depth-first search (DFS) approach.

The dfs function performs the recursive DFS traversal and maintains a path list that holds the values of nodes encountered along the current path. It checks for valid K-sum paths ending at the current node by calculating the cumulative sum of values along the path and comparing it with the target sum. If a valid path is found, it prints the path.

The example usage section demonstrates how to find K-sum paths with a given target value in a binary tree.

Keep in mind that this implementation prints the paths as sequences of values. Depending on your use case, you can modify the implementation to return or store the paths in a more structured format if needed.

25. Tree - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?