From c159731fa397c66a54c58a29a76206ce37fd63c1 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 7 Jun 2021 10:28:06 +0800 Subject: [PATCH] =?UTF-8?q?109:=E6=9C=89=E5=BA=8F=E9=93=BE=E8=A1=A8?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConvertSortedListToBinarySearchTree.java | 84 +++++++++++++++++++ .../cn/ConvertSortedListToBinarySearchTree.md | 17 ++++ 2 files changed, 101 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.java create mode 100644 src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.md diff --git a/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.java b/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.java new file mode 100644 index 0000000..25cf787 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.java @@ -0,0 +1,84 @@ +//给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。 +// +// 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。 +// +// 示例: +// +// 给定的有序链表: [-10, -3, 0, 5, 9], +// +//一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树: +// +// 0 +// / \ +// -3 9 +// / / +// -10 5 +// +// Related Topics 深度优先搜索 链表 +// 👍 533 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; +import com.code.leet.entiy.TreeNode; + +//109:有序链表转换二叉搜索树 +public class ConvertSortedListToBinarySearchTree { + public static void main(String[] args) { + //测试代码 + Solution solution = new ConvertSortedListToBinarySearchTree().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * 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 sortedListToBST(ListNode head) { + if (head == null) { + return null; + } else if (head.next == null) { + return new TreeNode(head.val); + } + ListNode beforeMid = head; + ListNode mid = head; + ListNode last = head; + while (last != null && last.next != null) { + beforeMid = mid; + mid = mid.next; + last = last.next.next; + } + + beforeMid.next = null; + TreeNode root = new TreeNode(mid.val); + root.left = sortedListToBST(head); + root.right = sortedListToBST(mid.next); + + return root; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.md b/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.md new file mode 100644 index 0000000..34af21a --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ConvertSortedListToBinarySearchTree.md @@ -0,0 +1,17 @@ +

给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树。

+ +

本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1。

+ +

示例:

+ +
给定的有序链表: [-10, -3, 0, 5, 9],
+
+一个可能的答案是:[0, -3, 9, -10, null, 5], 它可以表示下面这个高度平衡二叉搜索树:
+
+      0
+     / \
+   -3   9
+   /   /
+ -10  5
+
+
Related Topics
  • 深度优先搜索
  • 链表
  • \n
  • 👍 533
  • 👎 0
  • \ No newline at end of file