Skip to content

backtracking.py #10444

@vibhorjoshi

Description

@vibhorjoshi

Feature description

class TreeNode:
def init(self, value):
self.value = value
self.left = None
self.right = None

def find_paths(root):
def backtrack(node, path):
if not node:
return

    path.append(node.value)

    if not node.left and not node.right:  # Leaf node
        print(" -> ".join(map(str, path)))

    backtrack(node.left, path)
    backtrack(node.right, path)

    path.pop()  # Backtrack to explore other paths

if not root:
    return

backtrack(root, [])

Example usage

Construct a sample binary tree

root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)

Find and print all paths from root to leaf nodes

print("Paths from root to leaf nodes:")
find_paths(root)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementThis PR modified some existing files

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions