-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Binary tree path sum #7748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Binary tree path sum #7748
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59760a8
feat: Implement binary tree path sum (#7135)
CaedenPH 7ca71ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c359807
Merge branch 'TheAlgorithms:master' into binary-tree-path-sum
CaedenPH 897f4c5
Update data_structures/binary_tree/binary_tree_path_sum.py
CaedenPH 02e8327
refactor: Rename `dfs` to `depth_first_search`
CaedenPH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
""" | ||
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/ | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
|
||
class Node: | ||
""" | ||
A Node has value variable and pointers to Nodes to its left and right. | ||
""" | ||
|
||
def __init__(self, value: int) -> None: | ||
self.value = value | ||
self.left: Node | None = None | ||
self.right: Node | None = None | ||
|
||
|
||
class BinaryTreePathSum: | ||
r""" | ||
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 | ||
""" | ||
|
||
target: int | ||
|
||
def __init__(self) -> None: | ||
self.paths = 0 | ||
|
||
def dfs(self, node: Node | None, path_sum: int) -> None: | ||
if node is None: | ||
return | ||
|
||
if path_sum == self.target: | ||
self.paths += 1 | ||
|
||
if node.left: | ||
self.dfs(node.left, path_sum + node.left.value) | ||
if node.right: | ||
self.dfs(node.right, path_sum + node.right.value) | ||
|
||
def path_sum(self, node: Node | None, target: int | None = None) -> int: | ||
if node is None: | ||
return 0 | ||
if target is not None: | ||
self.target = target | ||
|
||
self.dfs(node, node.value) | ||
self.path_sum(node.left) | ||
self.path_sum(node.right) | ||
|
||
return self.paths | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.