Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Problem: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public TreeNode sortedArrayToBST(int[] nums) {
- if(nums == null || nums.length == 0) {
- return null;
- }
- return constructBSTRecursive(nums, 0, nums.length - 1);
- }
- private TreeNode constructBSTRecursive(int[] nums, int left, int right) {
- if(left > right) {
- return null;
- }
- int mid = left + (right - left) / 2;
- TreeNode current = new TreeNode(nums[mid]);
- current.left = constructBSTRecursive(nums, left, mid - 1);
- current.right = constructBSTRecursive(nums, mid + 1, right);
- return current;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement