From f4a48e4181ed969100e60e0faa70e7cc8dfc737f Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Mon, 8 Jan 2024 22:34:50 -0500 Subject: 872 --- 872/main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 872/main.py (limited to '872') diff --git a/872/main.py b/872/main.py new file mode 100644 index 0000000..5bf1f6c --- /dev/null +++ b/872/main.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +import typing + + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + + +class Solution: + def traverse(self, node: typing.Optional[TreeNode], leafSeq): + if node is None: + return + + if node.left is None and node.right is None: + leafSeq.append(node.val) + return + + self.traverse(node.left, leafSeq) + self.traverse(node.right, leafSeq) + + def leafSimilar( + self, root1: typing.Optional[TreeNode], root2: typing.Optional[TreeNode] + ) -> bool: + leafSeq1: typing.List[int] = [] + leafSeq2: typing.List[int] = [] + + self.traverse(root1, leafSeq1) + self.traverse(root2, leafSeq2) + + if len(leafSeq1) != len(leafSeq2): + return False + + for e1, e2 in zip(leafSeq1, leafSeq2): + if e1 != e2: + return False + + return True + + +def main(): + solution = Solution() + # print(solution.leafSimilar()) + + +if __name__ == "__main__": + main() -- cgit v1.2.3