forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArrayToBST.java
More file actions
37 lines (33 loc) · 918 Bytes
/
SortedArrayToBST.java
File metadata and controls
37 lines (33 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package normal;
/**
* @program JavaBooks
* @description: 108. 将有序数组转换为二叉搜索树
* @author: mf
* @create: 2019/11/08 15:34
*/
/*
题目:
*/
public class SortedArrayToBST {
public static void main(String[] args) {
}
/**
* [Java] 二叉树中序遍历的逆过程哦
* @param nums
* @return
*/
private static TreeNode sortedArrayToBST(int[] nums) {
// 左右等分建立左右子树,中间节点作为子树根节点,递归该过程
return nums == null ? null : buildTree(nums, 0, nums.length - 1);
}
private static TreeNode buildTree(int[] nums, int l, int r) {
if (l > r) {
return null;
}
int m = l + (r - l) / 2;
TreeNode root = new TreeNode(nums[m]);
root.left = buildTree(nums, l, m - 1);
root.right = buildTree(nums, m + 1, r);
return root;
}
}