feat: Binary tree node sum (#7020) - #7162
Conversation
for more information, see https://pre-commit.ci
cclauss
left a comment
There was a problem hiding this comment.
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.
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 <cclauss@me.com>
|
@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 |

Describe your change:
Implements #7020
Algorithm to get the sum of all tree nodes
Checklist:
Fixes: #{$ISSUE_NO}.