Leaf-Similar Trees LeetCode solution in Python

1
# class TreeNode:
2
# def __init__(self, val=0, left=None, right=None):
3
# self.val = val
4
# self.left = left
5
# self.right = right
6
class Solution:
7
def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
8
tree = []
9

10
def inorder(root):
11
nonlocal tree
12
if root is None:
13
return
14
if root.left is None and root.right is None:
15
tree.append(root.val)
16

17
inorder(root.left)
18
inorder(root.right)
19

20
inorder(root1)
21
inorder(root2)
22
tree1 = tree[: len(tree) // 2]
23
tree2 = tree[len(tree) // 2 :]
24
if tree1 == tree2:
25
return True
26
else:
27
return False

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0