✅ DFS | Short & Straightforward
\n\n# Solution\nswift []\nstruct Solution {\n    func treeQueries(_ root: TreeNode?, _ queries: [Int]) -> [Int] {\n        var heights = [Int](repeating: 0, cou
![Screenshot.png](https://assets.leetcode.com/users/images/3d2134cd-3e94-4947-81c6-7d500406c1a8_1729931110.9535193.png)\n\n# Solution\n```swift []\nstruct Solution {\n    func treeQueries(_ root: TreeNode?, _ queries: [Int]) -> [Int] {\n        var heights = [Int](repeating: 0, count: 100001)\n        var maxHeight = 0\n\n        func preorder(_ node: TreeNode?, _ height: Int = 0, isLeftToRight: Bool) {\n            guard let node else { return }\n            heights[node.val] = max(heights[node.val], maxHeight)\n            maxHeight = max(maxHeight, height)\n            let nodes = isLeftToRight ? (node.left, node.right) : (node.right, node.left)\n            preorder(nodes.0, height + 1, isLeftToRight: isLeftToRight)\n            preorder(nodes.1, height + 1, isLeftToRight: isLeftToRight)\n        }\n        preorder(root, isLeftToRight: true)\n        maxHeight = 0\n        preorder(root, isLeftToRight: false)\n        return queries.reduce(into: [Int]()) { r, q in\n            r.append(heights[q])\n        }\n    }\n}\n```
['Array', 'Depth-First Search', 'Swift', 'Binary Tree']