Swift solution - BFS


  • 0
    class Solution {
        func findBottomLeftValue(_ root: TreeNode?) -> Int {
            guard let root = root else {
                return 0
            }
            
            var queue = [TreeNode]()
            var node = root
            
            queue.append(node)
            while !queue.isEmpty {
                node = queue.removeFirst()
                if let rightNode = node.right {
                    queue.append(rightNode)
                }
                if let leftNode = node.left {
                    queue.append(leftNode)
                }
            }
            
            return node.val
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.