-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
feat: Binary tree node sum (#7020) #7162
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
Conversation
for more information, see https://pre-commit.ci
@cclauss pinging for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you want to try the extra credit bit or not.
def __init__(self, tree: Node) -> None: | ||
self.tree = tree | ||
|
||
def depth_first_search(self, node: Node | None) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from collections.abc import Iterator
Extra credit: def __iter__(self) -> Iterator[int]
https://docs.python.org/3/reference/datamodel.html#object.__iter__
https://github.com/TheAlgorithms/Python/search?q=__iter__
Then you could replace node_sum()
with sum(binary_tree_node_sum)
.
Co-authored-by: Christian Clauss <[email protected]>
@cclauss |
No, that's not what he meant. He's asking you to implement the iteration protocol for a custom Python object. It comprises of two methods These methods would be made available to the sum(node.value for node in tree) If you need a bit more understanding, I could help you out with an example. A class for doing a sum seems redundant. Alternatively, you could just create a function instead. |
I would just define |
Okay, I understand. However, perhaps all of this should be implemented in the basic binary tree or some other more general file instead of binary tree path sum |
Describe your change:
Implements #7020
Algorithm to get the sum of all tree nodes
Checklist:
Fixes: #{$ISSUE_NO}
.