Created
November 8, 2023 02:43
-
-
Save jianminchen/d7a4701930a6a4b3dac1a1df2ba96f41 to your computer and use it in GitHub Desktop.
2476 Closest nodes queries in a binary search tree - TLE error, tree's height is O(N), not O(logN). It is better to save in the array, and apply binary search
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace _2476_closest_nodes | |
{ | |
class Program | |
{ | |
public class TreeNode | |
{ | |
public int val; | |
public TreeNode left; | |
public TreeNode right; | |
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) | |
{ | |
this.val = val; | |
this.left = left; | |
this.right = right; | |
} | |
} | |
static void Main(string[] args) | |
{ | |
/* | |
Input: root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], | |
queries = [2,5,16] | |
Output: [[2,2],[4,6],[15,-1]] | |
* */ | |
var test = new Program(); | |
var root = new TreeNode(6); | |
root.left = new TreeNode(2); | |
root.left.left = new TreeNode(1); | |
root.left.right = new TreeNode(4); | |
root.right = new TreeNode(13); | |
root.right.left = new TreeNode(9); | |
root.right.right = new TreeNode(15); | |
root.right.right.left = new TreeNode(14); | |
var result = test.ClosestNodes(root, new int[]{2, 5, 16}.ToList()); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="root"></param> | |
/// <param name="queries"></param> | |
/// <returns></returns> | |
public IList<IList<int>> ClosestNodes(TreeNode root, IList<int> queries) | |
{ | |
var list = new List<IList<int>>(); | |
if (root == null || queries == null || queries.Count == 0) | |
{ | |
return list; | |
} | |
foreach (var item in queries) | |
{ | |
var number = findBiggestSmaller(root, item); | |
var number2 = findSmallestLarger(root, item); | |
list.Add(new int[] { number, number2 }.ToList()); | |
} | |
return list; | |
} | |
/// <summary> | |
/// Find biggest smaller in binary search tree | |
/// </summary> | |
/// <param name="root"></param> | |
/// <param name="number"></param> | |
/// <returns></returns> | |
private int findBiggestSmaller(TreeNode root, int number) | |
{ | |
if (root == null) | |
{ | |
return -1; | |
} | |
var value = root.val; | |
if (value == number) | |
{ | |
return number; | |
} | |
var found = -1; | |
if (value < number) | |
{ | |
found = value; | |
var result = findBiggestSmaller(root.right, number); | |
if (result == -1) | |
{ | |
return found; | |
} | |
return result; | |
} | |
else | |
{ | |
var result = findBiggestSmaller(root.left, number); | |
if (result == -1) | |
{ | |
return -1; | |
} | |
return result; | |
} | |
} | |
private int findSmallestLarger(TreeNode root, int number) | |
{ | |
if (root == null) | |
{ | |
return -1; | |
} | |
var value = root.val; | |
if (value == number) | |
{ | |
return number; | |
} | |
var found = -1; | |
if (value > number) | |
{ | |
found = value; | |
var result = findSmallestLarger(root.left, number); | |
if (result == -1) | |
{ | |
return found; | |
} | |
return result; | |
} | |
else | |
{ | |
var result = findSmallestLarger(root.right, number); | |
if (result == -1) | |
{ | |
return -1; | |
} | |
return result; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment