0
点赞
收藏
分享

微信扫一扫

LeetCode 将有序数组转换为二叉搜索树

夏沐沐 2022-02-28 阅读 28


https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/

我的解决方案:其实我也想到了是递归,但是一直没有找到循环体应该是什么样子的,后来搜索了一下BST,又根据题目中高度平衡这一关键字的点醒,逐渐找到了正确的思路

public class Solution {
public TreeNode recurison(int[] nums, int l, int r) {
if(l>r)
return null;
if(l==r)
return new TreeNode(nums[l]);
int m = (l+r+1)/2;
TreeNode root=new TreeNode(nums[m]);
root.left=recurison(nums, l,m-1);
root.right=recurison(nums, m+1, r);
return root;
}
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length==0) return null;
return recurison(nums, 0, nums.length-1);
}
}

我的思路:

/* -----------------------------------
* WARNING:
* -----------------------------------
* Your code may fail to compile
* because it contains public class
* declarations.
* To fix this, please remove the
* "public" keyword from your class
* declarations.
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}


//这道题的关键是折半查找、找中值
//下面的思路是错误的,这是一颗二叉搜索树、而且要求平衡
//找待处理数组中的数字的中值
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length==0) return null;
if(nums.length==1) return new TreeNode(nums[0]);

从数组中选择中值
然后分别在左侧和右侧找中值
将找到的中值分别作为root的左节点和右节点
然后再再左边的中值分成的两部分中寻找左侧中值和右侧中值
右边也进行相同的操作


写一个寻找中值的函数,每次传入参数均为nums,还有两个数组指针l和r
((l+r+1)/2)
public TreeNode recurison(int[] nums, int l, int r){
if(l==r)
return new TreeNode(nums[l]);
TreeNode root=new TreeNode(nums[(l+r+1)/2];
root.left=recurison(nums, l,m-1);
root.right=recurison(nums, m+1, r);
return root;
}
int m;
root.left=new Treenode(0,m-1)
root.right=new Treenode(m+1, nums.length-1)



//数据源是已经排好序的数组
//找到该数组的中值,然后向左向右遍历
//根据题目中对高度平衡二叉树的描述,我们可以直接按照二叉树的层次
//进行放置,其实和上道题目是比较接近的

//使用队列,需要对传进来的数组参数进行预处理
int[] num = new int[nums.length];
int cnt=0;
if(nums.length%2==1) {
num[cnt++]=nums[nums.length/2];
for(int i=nums.length/2-1,j=nums.length/2+1;i>=0&&j<nums.length;i--,j++) {
num[cnt++]=nums[i];
num[cnt++]=nums[j];
}
int index=0;
TreeNode root = new TreeNode(num[index++]);

Queue<TreeNode> q = new LinkedList();

if(index<num.length)
root.left=new TreeNode(num[index++]);
else
return root;
q.offer(root.left);
if(index<num.length)
root.right=new TreeNode(num[index++]);
else
return root;
q.offer(root).right);

while(index<num.length) {
TreeNode temp = q.poll();
temp.left=new TreeNode(num[index++]);
q.offer(temp.left);
if(index<num.length) {
temp = q.poll();
temp.right=new TreeNode(num[index++]);
q.offer(temp.right);
}
}
return root;
}
else {
for(int i=nums.length/2-1,j=nums.length/2;i>=0&&j<nums.length;i--,j++) {
num[cnt++]=nums[i];
num[cnt++]=nums[j];
}


int index=0;
TreeNode root = new TreeNode(num[index++]);

Queue<TreeNode> q = new LinkedList();

if(index<num.length)
root.right=new TreeNode(num[index++]);
else
return root;
q.offer(root.right);
if(index<num.length)
root.left=new TreeNode(num[index++]);
else
return root;
q.offer(root.left);

while(index<num.length) {
TreeNode temp = q.poll();
temp.right=new TreeNode(num[index++]);
q.offer(temp.right);
if(index<num.length) {
temp = q.poll();
temp.left=new TreeNode(num[index++]);
q.offer(temp.left);
}
}
return root;
}
}
}

public class MainClass {
public static int[] stringToIntegerArray(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return new int[0];
}

String[] parts = input.split(",");
int[] output = new int[parts.length];
for(int index = 0; index < parts.length; index++) {
String part = parts[index].trim();
output[index] = Integer.parseInt(part);
}
return output;
}

public static String treeNodeToString(TreeNode root) {
if (root == null) {
return "[]";
}

String output = "";
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
while(!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();

if (node == null) {
output += "null, ";
continue;
}

output += String.valueOf(node.val) + ", ";
nodeQueue.add(node.left);
nodeQueue.add(node.right);
}
return "[" + output.substring(0, output.length() - 2) + "]";
}

public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
int[] nums = stringToIntegerArray(line);

TreeNode ret = new Solution().sortedArrayToBST(nums);

String out = treeNodeToString(ret);

System.out.print(out);
}
}
}



举报

相关推荐

0 条评论