The issue with your code lies in how it constructs the binary search tree (BST). Specifically, the tree you build does not maintain the BST property, which requires that for every node, all elements in its left subtree are less than the node's value, and all elements in its right subtree are greater.
In your implementation, when adding nodes to the left and right subtrees, you do not check whether you're placing a smaller value on the left or a larger value on the right. Instead, you sequentially add nodes based on the order in the array, which can lead to incorrect placements that violate the BST property.
Here's a breakdown of the problem with your code:
-
Incorrect Node Placement: When adding nodes using the currentNode.left and currentNode.right pointers, you're not considering whether the new node's value is less than or greater than the currentNode's value. For example, in your left subtree construction, you might place a smaller value as the right child of a node, which is incorrect in a BST.
-
Violation of BST Property: The BST property is essential for the tree to be considered a valid binary search tree. By not maintaining this property, the structure of the tree does not fulfill the requirements specified in the problem description.
-
Expected Tree Structure: LeetCode expects the tree to not only be height-balanced but also a valid BST. Even if the tree you create is balanced, it must also satisfy the BST ordering property.
To fix this, you need to construct the tree by recursively selecting the middle element of the current subarray to ensure that all left children are less than the parent node and right children are greater. This approach guarantees that the BST property is maintained throughout the tree.
Here's a corrected version of your function:
function sortedArrayToBST(nums: number[]): TreeNode | null {
if (!nums.length) return null;
function buildBST(left: number, right: number): TreeNode | null {
if (left > right) return null;
const mid = Math.floor((left + right) / 2);
const node = new TreeNode(nums[mid]);
node.left = buildBST(left, mid - 1);
node.right = buildBST(mid + 1, right);
return node;
}
return buildBST(0, nums.length - 1);
}
This recursive function correctly constructs a height-balanced BST by always choosing the middle element as the root of the current subtree and building left and right subtrees from the remaining elements. It ensures that all nodes in the left subtree are less than the current node and all nodes in the right subtree are greater, maintaining the BST property.
Conclusion:
Your original code does not construct a valid BST because it doesn't maintain the ordering required by the BST property. By modifying your approach to recursively build the tree and choosing the middle element as the root at each step, you can create a height-balanced BST that will be accepted by LeetCode.
What's wrong is that your code violates the BST property—it doesn’t ensure left children are less and right children are greater than their parent—so it doesn’t build a valid binary search tree.