Swift solution - DFS, BFS


  • 0
    class Solution {
        func maxDepth_DFS(_ root: TreeNode?) -> Int {
            guard let root = root else {
                return 0
            }
            
            return max(maxDepth(root.left), maxDepth(root.right)) + 1
        }
        
        func maxDepth_BFS(_ root: TreeNode?) -> Int {
            guard let root = root else {
                return 0
            }
            
            var maxDepth = 0
            var queue = [TreeNode]()
            
            queue.append(root)
            while !queue.isEmpty {
                let count = queue.count
                for _ in 0..<count {
                    let node = queue.removeFirst()
                    if let leftNode = node.left {
                        queue.append(leftNode)
                    }
                    if let rightNode = node.right {
                        queue.append(rightNode)
                    }
                }
                maxDepth += 1
            }
            
            return maxDepth
        }
    }
    

Log in to reply
 

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