Skip to content
Open
Prev Previous commit
Next Next commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 2, 2023
commit 49633c2fd2ef00d791cf32bd2e71171aac9e595b
5 changes: 1 addition & 4 deletions data_structures/binary_tree/binary_tree_maximum_path_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __init__(self, val: int) -> None:


class GetMaxPathSum:
Copy link
Member

@cclauss cclauss Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a custom class only designed the get the sum.

I would prefer to see a generic BinaryTree class that:

  1. has a self.root_node
  2. implements .__iter__() instead of .traverse().
  3. delivers the solution with the simple line sum(binary_tree)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps 3. is not possible for this problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Now I see the difficulties. Please make the BinaryTree and make a .traverse() method that yields the values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are probably right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cclauss Hello, My PR getting out of the list now. Can you hear me back please how can I improve more.


r"""

GetMaxPathSum takes root node of a tree as initial argument.
Expand Down Expand Up @@ -67,7 +66,6 @@ def __init__(self, root) -> None:
self.root = root

def traverse(self, root: TreeNode | None) -> int:

"""
Returns maximum path sum by recursively taking max_path_sum from left
and max_path_sum from right if current Node has a left or right Node.
Expand All @@ -88,7 +86,6 @@ def traverse(self, root: TreeNode | None) -> int:
return root.val + max(right_sum, left_sum)

def max_path_sum(self) -> int:

"""
Driver method to get max_path_sum by calling traverse method.
:return max_path_sum:
Expand Down Expand Up @@ -124,7 +121,7 @@ def construct_tree() -> TreeNode:
return root


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

tree = GetMaxPathSum(construct_tree())
Expand Down