-
-
Notifications
You must be signed in to change notification settings - Fork 51k
Algorithm to get maximum path sum of a binary tree. #9414
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
Changes from all commits
5c091aa
969d2a4
b238cbb
1b21224
3692057
dc26a05
caf34e1
c1575a1
49633c2
cc7db5c
c31fc3f
16e7f01
ed46c53
868e4d4
007ad34
ce58ecc
d95f1c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
cclauss marked this conversation as resolved.
|
||
|
|
||
|
|
||
| # Leetcode Reference : https://leetcode.com/problems/binary-tree-maximum-path-sum/ | ||
| @dataclass | ||
| class TreeNode: | ||
|
cclauss marked this conversation as resolved.
|
||
| val: int | ||
| left: TreeNode | None = None | ||
| right: TreeNode | None = None | ||
|
|
||
|
|
||
| class GetMaxPathSum: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this but for a BinaryTree instead of a LinkedList.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps 3. is not possible for this problem.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are probably right.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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() = }") | ||
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.
This line is not required on a Python 3.14t codebase.