Medium草稿★★★★★O(n) 時間 · O(n) 空間
TreeBFSDFS
Patterns🌊 廣度優先 BFS🌳 樹形 DFS
尚未複習過

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

513Find Bottom Left Tree ValueTreesMediumTrees

給定一棵二元樹的根節點,回傳樹的最底層最左邊的值。

Example:

Input: root = [2,1,3] Output: 1

Intuition

TIP

BFS 從右到左遍歷,最後一個訪問的節點就是最底層最左邊的。

Approaches

1. BFS Level-order — O(n) / O(n)
  • Idea: 標準 BFS,每層記錄第一個節點的值,最後一層的第一個就是答案
  • Time: O(n)
  • Space: O(n)
class Solution {
    fun findBottomLeftValue(root: TreeNode?): Int {
        var result = root!!.`val`
        val queue: Queue<TreeNode> = LinkedList()
        queue.offer(root)
        while (queue.isNotEmpty()) {
            val size = queue.size
            for (i in 0 until size) {
                val node = queue.poll()
                if (i == 0) result = node.`val`
                node.left?.let { queue.offer(it) }
                node.right?.let { queue.offer(it) }
            }
        }
        return result
    }
}
⭐ 2. BFS Right-to-Left — O(n) / O(n)
  • Idea: 每次先加右子再加左子,最後出隊的就是最底層最左邊的節點
  • Time: O(n)
  • Space: O(n)
class Solution {
    fun findBottomLeftValue(root: TreeNode?): Int {
        var result = root!!.`val`
        val queue: Queue<TreeNode> = LinkedList()
        queue.offer(root)
        while (queue.isNotEmpty()) {
            val node = queue.poll()
            result = node.`val`
            node.right?.let { queue.offer(it) }
            node.left?.let { queue.offer(it) }
        }
        return result
    }
}
Why right-to-left BFS yields the answer last?

BFS 逐層遍歷。如果每層先加右子再加左子,那麼同一層中左邊的節點會排在右邊的後面。整棵樹最後被訪問的節點,就是最深層的最左邊節點。這個技巧省去了按層分隔的邏輯。

🔑 Takeaways