-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathL0109_ConvertSortedListToBinarySearchTree.java
124 lines (106 loc) · 3.9 KB
/
L0109_ConvertSortedListToBinarySearchTree.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import common.ListNode;
/**
* https://leetcode.cn/problems/convert-sorted-list-to-binary-search-tree/
*
* 给定一个单链表的头节点 head ,其中的元素 按升序排序 ,将其转换为高度平衡的二叉搜索树。
* 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。
*
* 示例 1:
* 
* 输入:head = [-10,-3,0,5,9]
* 输出:[0,-3,9,-10,null,5]
* 解释:一个可能的答案是[0,-3,9,-10,null,5],它表示所示的高度平衡的二叉搜索树。
*
* 示例 2:
* 输入:head = []
* 输出:[]
*
* 提示:
* - head 中的节点数在[0, 2 * 10⁴]范围内
* - -10⁵ <= Node.val <= 10⁵
*/
public class L0109_ConvertSortedListToBinarySearchTree {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
toString(this, sb);
return sb.toString();
}
private void toString(TreeNode node, StringBuilder sb) {
if (node == null) {
sb.append("null");
return;
}
sb.append(node.val);
if (node.left != null || node.right != null) {
sb.append(",");
toString(node.left, sb);
sb.append(",");
toString(node.right, sb);
}
}
}
public TreeNode sortedListToBST(ListNode head) {
// 如果链表为空,返回 null
if (head == null) {
return null;
}
// 如果链表只有一个节点,直接返回包含该值的树节点
if (head.next == null) {
return new TreeNode(head.val);
}
// 使用快慢指针找到链表的中间节点
ListNode slow = head;
ListNode fast = head;
ListNode prev = null;
while (fast != null && fast.next != null) {
fast = fast.next.next;
prev = slow;
slow = slow.next;
}
// 断开链表,分成左右两部分
prev.next = null;
// 创建根节点
TreeNode root = new TreeNode(slow.val);
// 递归构建左右子树
root.left = sortedListToBST(head);
root.right = sortedListToBST(slow.next);
return root;
}
public static void main(String[] args) {
L0109_ConvertSortedListToBinarySearchTree solution = new L0109_ConvertSortedListToBinarySearchTree();
// 测试用例 1:普通有序链表
ListNode head1 = new ListNode(-10);
head1.next = new ListNode(-3);
head1.next.next = new ListNode(0);
head1.next.next.next = new ListNode(5);
head1.next.next.next.next = new ListNode(9);
System.out.println("测试用例 1:");
System.out.println("输入:head = " + head1);
System.out.println("输出:" + solution.sortedListToBST(head1));
System.out.println();
// 测试用例 2:空链表
System.out.println("测试用例 2:");
System.out.println("输入:head = null");
System.out.println("输出:" + solution.sortedListToBST(null));
System.out.println();
// 测试用例 3:单个节点的链表
ListNode head3 = new ListNode(0);
System.out.println("测试用例 3:");
System.out.println("输入:head = " + head3);
System.out.println("输出:" + solution.sortedListToBST(head3));
}
}