Advertisement
unknown_0711

Untitled

Nov 1st, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. class Solution {
  2. public TreeNode sortedArrayToBST(int[] num) {
  3. if(num.length==0){
  4. return null;
  5. }
  6. TreeNode head = helper(num,0,num.length-1);
  7. return head;
  8. }
  9. public TreeNode helper(int[] num, int low , int high){
  10. if(low>high){
  11. return null;
  12.  
  13. }
  14. int mid = low+(high-low)/2;
  15. TreeNode node = new TreeNode(num[mid]);
  16. node.left = helper(num,low,mid-1);
  17. node.right = helper(num,mid+1,high);
  18. return node;
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement