首页 > 其他分享 >108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

时间:2022-11-19 19:23:09浏览次数:36  
标签:Binary Search TreeNode helper int Tree high num low

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

 

class Solution {     public TreeNode sortedArrayToBST(int[] nums) {         if (nums.length == 0) return null;         TreeNode head = helper(nums, 0, nums.length - 1);         return head;     }     public TreeNode helper(int[] num, int low, int high) {         if (low > high)     return null;         int mid = low + (high - low) / 2;         TreeNode node = new TreeNode(num[mid]);         node.left = helper(num, low, mid - 1);         node.right = helper(num, mid + 1, high);         return node;     } }

标签:Binary,Search,TreeNode,helper,int,Tree,high,num,low
From: https://www.cnblogs.com/MarkLeeBYR/p/16906818.html

相关文章