Problem statement

https://binarysearch.com/problems/Subtree/

Solution

Equal to Leetcode 0572 Subtree of Another Tree.

Complexity

Time and space is O(m + n).

Code

class Solution:
    def solve(self, s, t):
        def dfs(root):
            if not root: return "#"
            return str(root.val) + ":" + dfs(root.left) + ":" + dfs(root.right)
        return ":" + dfs(t) in ":" + dfs(s)