Medium草稿★★★★★O(n) 時間 · O(1) 空間
TreeBFSBinary Tree
Patterns🌊 廣度優先 BFS
尚未複習過

解法已隱藏 — 先讀題目敘述、自己想想看,再點上方按鈕揭曉。

116Populating Next Right Pointers in Each NodeBinary SearchMediumBinary Search

給定一棵完美二元樹(perfect binary tree),將每個節點的 next 指標指向其右邊的節點。如果右邊沒有節點,則 nextnull

Example:

Input: root = [1,2,3,4,5,6,7] Output: [1,#,2,3,#,4,5,6,7,#](# 代表 next 為 null 的層末端)

Intuition

TIP

利用已建立的 next 指標做層序遍歷,不需要額外的佇列空間。

Approaches

1. BFS(Queue) — O(n) / O(n)
  • Idea: 標準 BFS 層序遍歷,同一層的節點依序連接 next 指標
  • Time: O(n)
  • Space: O(n)(佇列)
BFS Code
class Solution {
    fun connect(root: Node?): Node? {
        if (root == null) return null
        val queue: ArrayDeque<Node> = ArrayDeque()
        queue.add(root)
        while (queue.isNotEmpty()) {
            val size = queue.size
            for (i in 0 until size) {
                val node = queue.removeFirst()
                if (i < size - 1) {
                    node.next = queue.first()
                }
                node.left?.let { queue.add(it) }
                node.right?.let { queue.add(it) }
            }
        }
        return root
    }
}
⭐ 2. Level-order via Next Pointers — O(n) / O(1)
  • Idea: 用當前層已建立的 next 指標遍歷,為下一層建立 next 連接
  • Time: O(n)
  • Space: O(1)
class Solution {
    fun connect(root: Node?): Node? {
        var leftmost = root
        while (leftmost?.left != null) {
            var current = leftmost
            while (current != null) {
                // 連接同一父節點的左右子節點
                current.left!!.next = current.right
                // 連接不同父節點的子節點
                if (current.next != null) {
                    current.right!!.next = current.next!!.left
                }
                current = current.next
            }
            leftmost = leftmost.left
        }
        return root
    }
}

WARNING

完美二元樹保證每個非葉節點都有左右子節點。外層迴圈走最左邊節點(往下一層),內層迴圈透過 next 橫向遍歷同一層。

🔑 Takeaways