Determine if two binary trees are equal

前端 未结 7 1855
臣服心动
臣服心动 2020-12-04 22:33

What would be the efficient algorithm to find if two given binary trees are equal - in structure and content?

7条回答
  •  有刺的猬
    2020-12-04 23:08

    Modulo stack overflow, something like

    eq(t1, t2) =
        eq(t1.left, t2.left) && t1.data=t2.data && eq(t1.right, t2.right)
    

    (This generalizes to an equality predicate for all tree-structured algebraic data types - for any piece of structured data, check if each of its sub-parts are equal to each of the other one's sub-parts.)

提交回复
热议问题