Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@
* [Basic Binary Tree](data_structures/binary_tree/basic_binary_tree.py)
* [Binary Search Tree](data_structures/binary_tree/binary_search_tree.py)
* [Binary Search Tree Recursive](data_structures/binary_tree/binary_search_tree_recursive.py)
* [Binary Tree Maximum Path Sum](data_structures/binary_tree/binary_tree_maximum_path_sum.py)
* [Binary Tree Mirror](data_structures/binary_tree/binary_tree_mirror.py)
* [Binary Tree Node Sum](data_structures/binary_tree/binary_tree_node_sum.py)
* [Binary Tree Path Sum](data_structures/binary_tree/binary_tree_path_sum.py)
Expand Down Expand Up @@ -876,6 +877,7 @@
* [Test Factorial](maths/test_factorial.py)
* [Test Prime Check](maths/test_prime_check.py)
* [Three Sum](maths/three_sum.py)
* [Tonelli Shanks](maths/tonelli_shanks.py)
* [Trailing Zeroes](maths/trailing_zeroes.py)
* [Trapezoidal Rule](maths/trapezoidal_rule.py)
* [Triplet Sum](maths/triplet_sum.py)
Expand Down
116 changes: 116 additions & 0 deletions data_structures/binary_tree/binary_tree_maximum_path_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from __future__ import annotations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line is not required on a Python 3.14t codebase.


from dataclasses import dataclass
Comment thread
cclauss marked this conversation as resolved.


# Leetcode Reference : https://leetcode.com/problems/binary-tree-maximum-path-sum/
@dataclass
class TreeNode:
Comment thread
cclauss marked this conversation as resolved.
val: int
left: TreeNode | None = None
right: TreeNode | None = None


class GetMaxPathSum:

@cclauss cclauss Oct 2, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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.
Upon calling max_path_sum(), it returns maximum path
sum from the tree.

# Test

The below tree looks like this
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1

Result will be calculated like : 3 -> 3 -> 5 -> 10 -> -3 -> 11
As it is the maximum path possible.


>>> root = TreeNode(10)
>>> root.left = TreeNode(5)
>>> root.right = TreeNode(-3)
>>> root.left.left = TreeNode(3)
>>> root.left.right = TreeNode(2)
>>> root.right.right = TreeNode(11)
>>> root.left.left.left = TreeNode(3)
>>> root.left.left.right = TreeNode(-2)
>>> root.left.right.right = TreeNode(1)

>>> GetMaxPathSum(root).max_path_sum()
29
"""

def __init__(self, root: TreeNode) -> None:
self.sum = -9999999999
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.

:param root -> tree root:
:return int:
"""

if root is None:
return 0

right_sum = max(self.traverse(root.right), 0)
left_sum = max(self.traverse(root.left), 0)

val = root.val + right_sum + left_sum
self.sum = max(val, self.sum)

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:
"""
self.traverse(self.root)
return self.sum


def construct_tree() -> TreeNode:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

r"""
The below tree
-10
/ \
9 20
/ \
15 7

>>> root = TreeNode(-10)
>>> root.left = TreeNode(9)
>>> root.right = TreeNode(20)
>>> root.right.left = TreeNode(15)
>>> root.right.right = TreeNode(7)

>>> GetMaxPathSum(construct_tree()).max_path_sum()
42
"""

root = TreeNode(-10)
root.left = TreeNode(9)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)
return root


if __name__ == "__main__":
import doctest

doctest.testmod()

tree = GetMaxPathSum(construct_tree())
print(f"{tree.max_path_sum() = }")
Loading