Pseudocode to compare two trees

后端 未结 5 1977
猫巷女王i
猫巷女王i 2021-02-02 11:11

This is a problem I\'ve encountered a few times, and haven\'t been convinced that I\'ve used the most efficient logic.

As an example, presume I have two trees: one is a

5条回答
  •  离开以前
    2021-02-02 12:04

    Seems like you just want to do a pre-order traversal, essentially. Where "visiting" a node means checking for children that are in one version but not the other.

    More precisely: start at the root. At each node, get a set of items in each of the two versions of the node. The symmetric difference of the two sets contains the items in one but not the other. Print/output those. The intersection contains the items that are common to both. For each item in the intersection (I assume you aren't going to look further into the items that are missing from one tree), call "visit" recursively on that node to check its contents. It's a O(n) operation, with a little recursion overhead.

提交回复
热议问题