Algorithm to get maximum path sum of a binary tree. - #9414
Conversation
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| left, right -> Stores the pointer to left or right node. | ||
| """ | ||
|
|
||
| def __init__(self, val: int, left=None, right=None) -> None: |
There was a problem hiding this comment.
Please provide type hint for the parameter: left
Please provide type hint for the parameter: right
| 29 | ||
| """ | ||
|
|
||
| def __init__(self, root): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: root
| return self.sum | ||
|
|
||
|
|
||
| def construct_tree() -> TreeNode: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/binary_tree_maximum_path_sum.py, please provide doctest for the function construct_tree
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| left, right -> Stores the pointer to left or right node. | ||
| """ | ||
|
|
||
| def __init__(self, val: int, left=None, right=None) -> None: |
There was a problem hiding this comment.
Please provide type hint for the parameter: left
Please provide type hint for the parameter: right
| 29 | ||
| """ | ||
|
|
||
| def __init__(self, root): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide type hint for the parameter: root
| return self.sum | ||
|
|
||
|
|
||
| def construct_tree() -> TreeNode: |
There was a problem hiding this comment.
As there is no test file in this pull request nor any test function or class in the file data_structures/binary_tree/binary_tree_maximum_path_sum.py, please provide doctest for the function construct_tree
for more information, see https://pre-commit.ci
# Conflicts: # data_structures/binary_tree/binary_tree_maximum_path_sum.py
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| 29 | ||
| """ | ||
|
|
||
| def __init__(self, root) -> None: |
There was a problem hiding this comment.
Please provide type hint for the parameter: root
for more information, see https://pre-commit.ci
|
@cclauss Hello, sorry to interrupt ! , but can you please take a look into it. |
| self.right: TreeNode | None = None | ||
|
|
||
|
|
||
| class GetMaxPathSum: |
There was a problem hiding this comment.
This is a custom class only designed the get the sum.
I would prefer to see a generic BinaryTree class that:
- has a
self.root_node - implements
.__iter__()instead of.traverse(). - delivers the solution with the simple line
sum(binary_tree)
There was a problem hiding this comment.
Like this but for a BinaryTree instead of a LinkedList.
https://github.com/TheAlgorithms/Python/blob/master/data_structures/linked_list/reverse_k_group.py#L13
There was a problem hiding this comment.
Perhaps 3. is not possible for this problem.
There was a problem hiding this comment.
@cclauss I'm bit confused about iter(), as traverse recursively takes maximum from left and right subtree, how can I implement that. Please give me hint on it.
There was a problem hiding this comment.
Yes. Now I see the difficulties. Please make the BinaryTree and make a .traverse() method that yields the values.
There was a problem hiding this comment.
@cclauss As the algorithm traverses all the nodes recursively, if we try to yield values of nodes, we'll end up yielding all the nodes. Because yield will go over all nodes where's our algorithm only requires those nodes that makes up the maximum value. Please correct me if i'm wrong.
There was a problem hiding this comment.
@cclauss Hello, My PR getting out of the list now. Can you hear me back please how can I improve more.
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
for more information, see https://pre-commit.ci
| @@ -0,0 +1,118 @@ | |||
| from __future__ import annotations | |||
There was a problem hiding this comment.
This line is not required on a Python 3.14t codebase.
Describe your change:
Added a new algorithm to get maximum path sum of a binary tree.
It solves one of the Hard questions from Leetcode.
Checklist: