[Golang] using Binary Search Tree
Code\n\ntype Node struct {\n\tval   int\n\tleft  *Node\n\tright *Node\n}\n\ntype BST struct {\n\troot *Node\n}\n\nfunc NewBST() *BST {\n\treturn &BST{}\n}\n\nfu
# Code\n```\ntype Node struct {\n\tval   int\n\tleft  *Node\n\tright *Node\n}\n\ntype BST struct {\n\troot *Node\n}\n\nfunc NewBST() *BST {\n\treturn &BST{}\n}\n\nfunc (bst *BST) insert(v int) {\n\tn := &Node{\n\t\tval: v,\n\t}\n\tif bst.root == nil {\n\t\tbst.root = n\n\t\treturn\n\t}\n\n\ttemp := bst.root\n\tfor {\n\t\tif v < temp.val {\n\t\t\tif temp.left == nil {\n\t\t\t\ttemp.left = n\n\t\t\t\treturn\n\t\t\t}\n\t\t\ttemp = temp.left\n\t\t} else {\n\t\t\tif temp.right == nil {\n\t\t\t\ttemp.right = n\n\t\t\t\treturn\n\t\t\t}\n\t\t\ttemp = temp.right\n\t\t}\n\t}\n}\n\nfunc (bst *BST) search(v int) int {\n\tif bst.root == nil {\n\t\treturn -1\n\t}\n\n\ttemp := bst.root\n\tcloseResult := temp.val\n\n\tfor {\n\t\tif temp.val == v {\n\t\t\treturn v\n\t\t}\n\n\t\tif abs(v, temp.val) < abs(closeResult, v) {\n\t\t\tcloseResult = temp.val\n\t\t}\n\n\t\tif v < temp.val {\n\t\t\tif temp.left == nil {\n\t\t\t\treturn closeResult\n\t\t\t}\n\t\t\ttemp = temp.left\n\t\t} else {\n\t\t\tif temp.right == nil {\n\t\t\t\treturn closeResult\n\t\t\t}\n\t\t\ttemp = temp.right\n\t\t}\n\t}\n}\n\nfunc abs(a, b int) int {\n\tif a > b {\n\t\treturn a - b\n\t}\n\treturn b - a\n}\n\nfunc min(a, b int) int {\n\tif a < b {\n\t\treturn a\n\t}\n\treturn b\n}\n\ntype Query struct {\n\troomID int\n\tsize   int\n\tindex  int\n}\n\nfunc closestRoom(rooms [][]int, queries [][]int) []int {\n\n\tmyQueries := make([]Query, 0)\n\tfor i, query := range queries {\n\t\tmyQueries = append(myQueries, Query{\n\t\t\tsize:   query[1],\n\t\t\troomID: query[0],\n\t\t\tindex:  i,\n\t\t})\n\t}\n\n\t// sort queries in descending order based on required size\n\tsort.Slice(myQueries, func(i, j int) bool {\n\t\tif myQueries[i].size > myQueries[j].size {\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\t// sort rooms in descending order based on size\n\tsort.Slice(rooms, func(i, j int) bool {\n\t\tif rooms[i][1] > rooms[j][1] {\n\t\t\treturn true\n\t\t}\n\t\treturn false\n\t})\n\n\troomsIndex := 0\n\tbst := NewBST()\n\n\tresult := make([]int, len(myQueries))\n\tfor _, query := range myQueries {\n\t\trequiredSize := query.size\n\t\tfor ; roomsIndex < len(rooms) && rooms[roomsIndex][1] >= requiredSize; roomsIndex++ {\n\t\t\tbst.insert(rooms[roomsIndex][0])\n\t\t}\n\t\tres := bst.search(query.roomID)\n\t\tif res > query.roomID {\n\t\t\t// If we are searching for 5, and res=7, then we can check if 3 (i.e 5 - (7-5)) exists in bst as priority is given to small number\n\t\t\trequiredSize2 := query.roomID - (res - query.roomID)\n\t\t\tif res2 := bst.search(requiredSize2); res2 == requiredSize2 {\n\t\t\t\tres = res2\n\t\t\t}\n\t\t}\n\n\t\tresult[query.index] = res\n\t}\n\n\treturn result\n}\n```
['Go']