Skip to content

Commit

Permalink
Create 0108-convert-sorted-array-to-binary-search-tree.java
Browse files Browse the repository at this point in the history
  • Loading branch information
zhrfrd committed Feb 23, 2023
1 parent 01e0e5c commit d2a23a4
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions java/0108-convert-sorted-array-to-binary-search-tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return generateTree(nums, 0, nums.length - 1);
}

public TreeNode generateTree(int[] nums, int low, int high) {
if (low > high) {
return null;
}

int mid = low + ((high - low) / 2);
TreeNode node = new TreeNode(nums[mid]);

node.left = generateTree(nums, low, mid - 1);
node.right = generateTree(nums, mid + 1, high);

return node;
}
}

0 comments on commit d2a23a4

Please sign in to comment.