# LeetCode刷题记63-109. 有序链表转换二叉搜索树【查看解法】

## 109. 有序链表转换二叉搜索树

/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public 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;
*     }
* }
*/
class Solution {
public TreeNode F(List<Integer> list, int l, int r) {
if (l > r) {
return null;
}
if (l == r) {
return new TreeNode(list.get(l));
}
int m = (l + r) / 2;
TreeNode root = new TreeNode(list.get(m));
root.left = F(list, l, m - 1);
root.right = F(list, m + 1, r);
return root;
}
public TreeNode sortedListToBST(ListNode head) {
List<Integer> list = new ArrayList<Integer>();
while (head != null) {
}
return F(list, 0, list.size() - 1);
}
}


5/5
63/150

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

咸蛋黄麦芽糖小饼干

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

04-18 184

05-28 569
05-18 195
06-10 29
09-28
06-06
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客