iT邦幫忙

2025 iThome 鐵人賽

0
自我挑戰組

Java × LeetCode-30天日記系列 第 26

Day 26:Invert Binary Tree (LC #226)

  • 分享至 

  • xImage
  •  

題目理解
我的理解 : 將一棵樹的每個節點的左子樹與右子樹交換。
方法

  1. 當 Queue 不為空時:
  • 取出隊首節點 currentNode。
  • 交換 currentNode 的左右子節點。
  • 若交換後的左右子節點不為空,則將它們加入 Queue,以便後續處理。
    2.當所有節點處理完後,回傳整棵反轉後的樹。

https://ithelp.ithome.com.tw/upload/images/20251013/20169238LVEW5nFe6y.png

心得
這題的邏輯雖然不難但可以直觀地看到每一層的節點交換過程。


上一篇
Day 25 — Maximum Depth of Binary Tree (LC #104)
下一篇
Day 27:Binary Tree Level Order Traversal (LC #102)
系列文
Java × LeetCode-30天日記30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言