Binary tree right join goop
This commit is contained in:
+52
-1
@@ -4,6 +4,57 @@ import typing
|
||||
class ListNode:
|
||||
"""ListNode is the leetcode "standard library" type used in linked lists"""
|
||||
|
||||
def __init__(self, val=0, next: typing.Optional["ListNode"] = None): # no qa
|
||||
def __init__(self, val: int = 0, next: typing.Optional["ListNode"] = None): # no qa
|
||||
self.val = val
|
||||
self.next = next
|
||||
|
||||
|
||||
class Node:
|
||||
"""Node is the leetcode "standard library" type used in binary trees"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
val: int = 0,
|
||||
left: typing.Optional["Node"] = None,
|
||||
right: typing.Optional["Node"] = None,
|
||||
next: typing.Optional["Node"] = None, # no qa
|
||||
):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
self.next = next
|
||||
|
||||
# __repr__ was added by me
|
||||
def __repr__(self) -> str:
|
||||
filtered_parts = []
|
||||
|
||||
for key, value in [
|
||||
("val", self.val),
|
||||
("right", self.right),
|
||||
("left", self.left),
|
||||
("next", self.next),
|
||||
]:
|
||||
if value is not None:
|
||||
filtered_parts.append((key, value))
|
||||
|
||||
middle = ", ".join([f"{k}={v!r}" for k, v in filtered_parts])
|
||||
|
||||
return f"Node({middle})"
|
||||
|
||||
# __eq__ was added by me
|
||||
def __eq__(self, other: "Node") -> bool:
|
||||
return (
|
||||
other is not None
|
||||
and self.val == other.val
|
||||
and self.left == other.left
|
||||
and self.right == other.right
|
||||
and self.next == other.next
|
||||
)
|
||||
|
||||
# __list__ was added by me
|
||||
def __list__(self) -> list[int | None]:
|
||||
ret = [self.val]
|
||||
ret += self.right.__list__() if self.right is not None else [None]
|
||||
ret += self.left.__list__() if self.left is not None else [None]
|
||||
ret += self.next.__list__() if self.next is not None else [None]
|
||||
return ret
|
||||
|
||||
Reference in New Issue
Block a user