aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorterminaldweller <devi@terminaldweller.com>2024-01-09 03:34:50 +0000
committerterminaldweller <devi@terminaldweller.com>2024-01-09 03:34:50 +0000
commitf4a48e4181ed969100e60e0faa70e7cc8dfc737f (patch)
tree06b5b364834b3496ee47ad0bbb0c75f03dffac9a
parent872 (diff)
downloadleetcode-f4a48e4181ed969100e60e0faa70e7cc8dfc737f.tar.gz
leetcode-f4a48e4181ed969100e60e0faa70e7cc8dfc737f.zip
872
-rw-r--r--872/main.py49
1 files changed, 49 insertions, 0 deletions
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()