Binary Trees question. Checking for similar shape

后端 未结 5 598
刺人心
刺人心 2021-01-06 06:49

Hi I\'m stuck doing this, not sure how to go about it.

If I have two binary trees, how would I check if the have the same shape? Data in the nodes doesn\'t matter, j

5条回答
  •  时光取名叫无心
    2021-01-06 07:36

    Two Breadth First Searches in parallel would be one way.

    http://en.wikipedia.org/wiki/Breadth-first_search

    With BFS, you can examine all nodes a particular level of the tree at the same time. That way, if you don't distinguish between right and left children (i.e. if your tree is considered the same if a node's counterpart has one child, regardless of "right" or "left" child) you can handle that at that time.

提交回复
热议问题