recursive c#(sharp) accepted solution


  • 0
            public static TreeNode SortedArrayToBST(int[] nums)
            {
                return Insertion(nums, 0, nums.Length - 1);
            }
    
            public static TreeNode Insertion(int[] nums, int start, int end)
            {
                if (start > end) return null;
                int middle = start + (end - start) / 2;
                TreeNode node = new TreeNode(nums[middle]);
                node.left = Insertion(nums, start, middle - 1);
                node.right = Insertion(nums, middle+1, end);
                return node;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.