data_structures.binary_tree.binary_tree_path_sum

Given the root of a binary tree and an integer target, find the number of paths where the sum of the values along the path equals target.

Leetcode reference: https://leetcode.com/problems/path-sum-iii/

Classes

BinaryTreePathSum

The below tree looks like this

Node

A Node has value variable and pointers to Nodes to its left and right.

Module Contents

class data_structures.binary_tree.binary_tree_path_sum.BinaryTreePathSum
The below tree looks like this

10

/

5 -3

/

3 2 11

/

3 -2 1

>>> tree = Node(10)
>>> tree.left = Node(5)
>>> tree.right = Node(-3)
>>> tree.left.left = Node(3)
>>> tree.left.right = Node(2)
>>> tree.right.right = Node(11)
>>> tree.left.left.left = Node(3)
>>> tree.left.left.right = Node(-2)
>>> tree.left.right.right = Node(1)
>>> BinaryTreePathSum().path_sum(tree, 8)
3
>>> BinaryTreePathSum().path_sum(tree, 7)
2
>>> tree.right.right = Node(10)
>>> BinaryTreePathSum().path_sum(tree, 8)
2
path_sum(node: Node | None, target: int | None = None) int
paths = 0
target: int
class data_structures.binary_tree.binary_tree_path_sum.Node(value: int)

A Node has value variable and pointers to Nodes to its left and right.

left: Node | None = None
right: Node | None = None
value