Skip to content

Commit c31fc3f

Browse files
committed
Added dataclass decorator to TreeNode
2 parents cc7db5c + 49633c2 commit c31fc3f

1 file changed

Lines changed: 7 additions & 13 deletions

File tree

data_structures/binary_tree/binary_tree_maximum_path_sum.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,18 @@
1010
1111
Leetcode Reference : https://leetcode.com/problems/binary-tree-maximum-path-sum/
1212
"""
13+
from __future__ import annotations
14+
from dataclasses import dataclass
1315

1416

17+
@dataclass
1518
class TreeNode:
16-
17-
"""
18-
TreeNode has tree variables, val -> Stores value of the node
19-
left, right -> Stores the pointer to left or right node.
20-
"""
21-
22-
def __init__(self, val: int) -> None:
23-
self.val: int = val
24-
self.left: TreeNode | None = None
25-
self.right: TreeNode | None = None
19+
val: int
20+
left: TreeNode | None = None
21+
right: TreeNode | None = None
2622

2723

2824
class GetMaxPathSum:
29-
3025
r"""
3126
3227
GetMaxPathSum takes root node of a tree as initial argument.
@@ -67,7 +62,6 @@ def __init__(self, root) -> None:
6762
self.root = root
6863

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

9084
def max_path_sum(self) -> int:
91-
9285
"""
9386
Driver method to get max_path_sum by calling traverse method.
9487
:return max_path_sum:
@@ -133,3 +126,4 @@ def construct_tree() -> TreeNode:
133126
print("Given example output: ", max_sum)
134127

135128
doctest.testmod()
129+

0 commit comments

Comments
 (0)