-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSortedArrayToBST.java
More file actions
43 lines (38 loc) · 1.13 KB
/
Copy pathSortedArrayToBST.java
File metadata and controls
43 lines (38 loc) · 1.13 KB
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
38
39
40
41
42
43
package com.leetcode.easy;
import com.leetcode.base.TreeNode;
/**
*
* 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树。
*
* 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
*
* 示例:
*
* 给定有序数组: [-10,-3,0,5,9],
*
* 一个可能的答案是:[0,-3,9,-10,null,5],它可以表示下面这个高度平衡二叉搜索树:
*
* 0
* / \
* -3 9
* / /
* -10 5
*
* */
public class SortedArrayToBST {
//本质上为中序遍历的逆过程
public TreeNode sortedArrayToBST(int[] nums) {
return nums == null ? null : buildTree(nums, 0, nums.length - 1);
}
//选取中间结点作为根结点 依次递归构造左右子树
private TreeNode buildTree(int[] nums, int l, int r) {
if (l > r) {
return null;
}
int mid = l + (r - l) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = buildTree(nums, l , mid - 1);
node.right = buildTree(nums, mid + 1, r);
return node;
}
}