Invert a binary tree.
Example:
Trivia:
This problem was inspired by this original tweet by Max Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.
Related Topics: Tree
頗為知名的一題(笑),但基本上沒什麼難度,只需要互換節點左右兩邊的子樹即可。
實作上就是採用後序(post-order)的方式尋訪整棵樹,並在尋訪父節點時交換左右子樹。
我寫這篇就是為了分享這個,在 Ruby China 上看到如何用最少字元翻轉二元樹XDDDDD
本文作者: 辛西亞.Cynthia
本文連結: 辛西亞的技能樹 / hackmd 版本
版權聲明: 部落格中所有文章,均採用 姓名標示-非商業性-相同方式分享 4.0 國際 (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!