Determine if a tree contains the structure of another tree

Idea one:

Traverse according to the conventional method, find the same root node and compare the child nodes once, if it fails, check the next node

The time complexity is O(n*m)

Idea two:

Using the tree structure serialization method, serialize the two trees in the same way, and then use kmp to judge the substring, you can get

As a result, the time complexity is O(m+n)

The feasibility of this question is: the serialization and deserialization of number structures are in one-to-one correspondence


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325667739&siteId=291194637