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¶
The below tree looks like this |
|
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 >>> BinaryTreePathSum().path_sum(None, 0) 0 >>> BinaryTreePathSum().path_sum(tree, 0) 0
- The second tree looks like this
0
/
5 5
>>> tree2 = Node(0) >>> tree2.left = Node(5) >>> tree2.right = Node(5)
>>> BinaryTreePathSum().path_sum(tree2, 5) 4 >>> BinaryTreePathSum().path_sum(tree2, -1) 0 >>> BinaryTreePathSum().path_sum(tree2, 0) 1
- paths = 0¶
- target: int¶