# Exercise I:
# July 14, 2023

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
        res_1 = []
        res_2 = []

        self.dfs(root1, res_1)
        self.dfs(root2, res_2)

        return res_1 == res_2 


    def dfs(self, root, res):
        if not root:
            return 

        left = root.left 
        right = root.right 

        if not left and not right:
            res.append(root.val)
        self.dfs(root.left, res)
        self.dfs(root.right, res)
