-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented 'Closest Nodes Queries in a Binary Search Tree' challenge
- Loading branch information
Showing
5 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
LeetCode/LeetCode/Challenges/ClosestNodesQueriesInABinarySearchTree.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// ClosestNodesQueriesInABinarySearchTree.swift | ||
// LeetCode | ||
// | ||
// Created by William Boles on 23/02/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
//https://leetcode.com/problems/closest-nodes-queries-in-a-binary-search-tree/ | ||
struct ClosestNodesQueriesInABinarySearchTree { | ||
|
||
//Time: O( | ||
//Space: O( | ||
//binary tree | ||
//binary search tree | ||
//binary search | ||
//in-order | ||
//DFS | ||
//recursive | ||
//inout | ||
//array | ||
// | ||
//Solution Description: | ||
//As this is binary search tree, we know that an in-order traversal will produce a sorted array. With a sorted array we can | ||
//then perform two binary searches to find both the upper and lower bounds of each query item. | ||
func closestNodes(_ root: TreeNode?, _ queries: [Int]) -> [[Int]] { | ||
guard let root = root else { | ||
return [[Int]]() | ||
} | ||
|
||
var values = [Int]() | ||
inorder(root, &values) | ||
|
||
var result = [[Int]]() | ||
|
||
for query in queries { | ||
let min = findMax(values, query) | ||
let max = findMin(values, query) | ||
|
||
result.append([min, max]) | ||
} | ||
|
||
return result | ||
} | ||
|
||
private func inorder(_ root: TreeNode?, _ order: inout [Int]) { | ||
guard let root = root else { | ||
return | ||
} | ||
|
||
inorder(root.left, &order) | ||
order.append(root.val) | ||
inorder(root.right, &order) | ||
} | ||
|
||
private func findMax(_ values: [Int], _ target: Int) -> Int { | ||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result = -1 | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] <= target { //find the upper bounds that is less than or equal to target | ||
result = values[mid] //we know that values[mid] is a valid result so store it and check again | ||
left = mid + 1 | ||
} else { | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private func findMin(_ values: [Int], _ target: Int) -> Int { | ||
var left = 0 | ||
var right = values.count - 1 | ||
|
||
var result = -1 | ||
|
||
while left <= right { | ||
let mid = left + (right - left) / 2 //to avoid overflow | ||
|
||
if values[mid] >= target { //find the lower bounds that is greater than or equal to target | ||
result = values[mid] //we know that values[mid] is a valid result so store it anad check again | ||
right = mid - 1 | ||
} else { | ||
left = mid + 1 | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
LeetCode/LeetCodeTests/Tests/ClosestNodesQueriesInABinarySearchTreeTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// ClosestNodesQueriesInABinarySearchTreeTests.swift | ||
// LeetCodeTests | ||
// | ||
// Created by William Boles on 23/02/2024. | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import LeetCode | ||
|
||
final class ClosestNodesQueriesInABinarySearchTreeTests: XCTestCase { | ||
|
||
//MARK: - Tests | ||
|
||
func test_A() { | ||
let data = [6,2,13,1,4,9,15,nil,nil,nil,nil,nil,nil,14] | ||
let queries = [2,5,16] | ||
|
||
let root = TreeNode.deserialize(data) | ||
|
||
let result = ClosestNodesQueriesInABinarySearchTree().closestNodes(root, queries) | ||
|
||
XCTAssertEqual(result, [[2,2],[4,6],[15,-1]]) | ||
} | ||
|
||
func test_B() { | ||
let data = [4,nil,9] | ||
let queries = [3] | ||
|
||
let root = TreeNode.deserialize(data) | ||
|
||
let result = ClosestNodesQueriesInABinarySearchTree().closestNodes(root, queries) | ||
|
||
XCTAssertEqual(result, [[-1,4]]) | ||
} | ||
|
||
func test_C() { | ||
let data = [16,8,18,1,12,nil,20,nil,2,9,nil,nil,nil,nil,7] | ||
let queries = [8,14,285508,6] | ||
|
||
let root = TreeNode.deserialize(data) | ||
|
||
let result = ClosestNodesQueriesInABinarySearchTree().closestNodes(root, queries) | ||
|
||
XCTAssertEqual(result, [[8,8],[12,16],[20,-1],[2,7]]) | ||
} | ||
|
||
func test_D() { | ||
let data = [16,14,nil,4,15,1] | ||
let queries = [10,6,2,9] | ||
|
||
let root = TreeNode.deserialize(data) | ||
|
||
let result = ClosestNodesQueriesInABinarySearchTree().closestNodes(root, queries) | ||
|
||
XCTAssertEqual(result, [[4,14],[4,14],[1,4],[4,14]]) | ||
} | ||
|
||
func test_E() { | ||
let data = [9,6,14,nil,nil,13,20,12] | ||
let queries = [19,10,9,17,19,6,10,19,13,6] | ||
|
||
let root = TreeNode.deserialize(data) | ||
|
||
let result = ClosestNodesQueriesInABinarySearchTree().closestNodes(root, queries) | ||
|
||
XCTAssertEqual(result, [[14,20],[9,12],[9,9],[14,20],[14,20],[6,6],[9,12],[14,20],[13,13],[6,6]]) | ||
} | ||
} |