/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
return arrToBST(num, 0, num.length-1);
}
public TreeNode arrToBST(int[] num, int l, int r){
if (l>r) return null;
TreeNode root = new TreeNode(num[(l+r)/2]);
root.left = arrToBST(num, l, (l+r)/2-1);
root.right = arrToBST(num, (l+r)/2+1, r);
return root;
}
}
Convert Sorted Array to Binary Search Tree
最新推荐文章于 2022-02-19 22:56:38 发布