0 Star 2 Fork 1

foolishbird-cn / arithmetic

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Solution.java 811 Bytes
一键复制 编辑 原始数据 按行查看 历史
mayi 提交于 2020-11-06 14:06 . 排序整理
package cn.foolishbird.arithmetic.leetcode.low.ConvertSortedArrayToBinarySearchTree;
import cn.foolishbird.arithmetic.leetcode.TreeNode;
import org.junit.Test;
/**
* @author Eric
* @version 1.0
* @date 2020-11-04
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return dfs(nums, 0, nums.length - 1);
}
public TreeNode dfs(int[] nums, int start, int end) {
if (start > end) {
return null;
}
int middle = (start + end + 1) / 2;
TreeNode root = new TreeNode(nums[middle]);
root.left = dfs(nums, start, middle - 1);
root.right = dfs(nums, middle + 1, end);
return root;
}
@Test
public void test() {
int[] nums = {-10, -3, 0, 5, 9};
sortedArrayToBST(nums);
}
}
Java
1
https://gitee.com/foolishbird-cn/arithmetic.git
git@gitee.com:foolishbird-cn/arithmetic.git
foolishbird-cn
arithmetic
arithmetic
master

搜索帮助