2024/8/30 Hot100 binaryTree 有bug待解决

This commit is contained in:
linlihong 2024-08-30 17:30:57 +08:00
parent 746e23928b
commit c92ea90fbb
2 changed files with 33 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import java.util.*;
* Hard 2
* Level ?
* @Description 昨日题今日做
* FinishTime: 2024/8/30/
* FinishTime: 2024/8/30/16:18
*/
public class Num102 {

View File

@ -0,0 +1,32 @@
package com.cool.hot100.binary_tree;
import org.junit.Test;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/30/16:17
* DayNumber 1
* Hard 1
* Level ?
*/
public class Num108 {
public TreeNode sortedArrayToBST(int[] nums) {
return buildTree(nums, 0, nums.length - 1);
}
public TreeNode buildTree(int[] nums, int start, int end) {
if(start>=end) return null;
TreeNode node=new TreeNode();
int index=(start+end)/2;
node.val=nums[index];
node.left=buildTree(nums,start,index-1);
node.right=buildTree(nums,index+1,end);
return node;
}
@Test
public void test(){
sortedArrayToBST(new int[]{-10,-3,0,5,9});
}
}