题意:

image.png

解题思路:

  1. 思路:O(log(n))
  2. 1. 快慢指针找到链表中间节点,用来作为树的根节点;
  3. 2. 递归链表左边构建左子树,递归右边构建右子树;

PHP代码实现:

  1. /**
  2. * Definition for a singly-linked list.
  3. * class ListNode {
  4. * public $val = 0;
  5. * public $next = null;
  6. * function __construct($val) { $this->val = $val; }
  7. * }
  8. */
  9. /**
  10. * Definition for a binary tree node.
  11. * class TreeNode {
  12. * public $val = null;
  13. * public $left = null;
  14. * public $right = null;
  15. * function __construct($value) { $this->val = $value; }
  16. * }
  17. */
  18. class Solution {
  19. /**
  20. * @param ListNode $head
  21. * @return TreeNode
  22. */
  23. function sortedListToBST($head) {
  24. //return $this->helper($head, null);
  25. $nums = [];
  26. while ($head != null) {
  27. $nums[] = $head->val;
  28. $head = $head->next;
  29. }
  30. return $this->helper($nums, 0, count($nums) - 1);
  31. }
  32. function helper($nums, $left, $right) {
  33. if ($left > $right) return null;
  34. $mid = $left + floor(($right - $left)/2);
  35. $node = new TreeNode($nums[$mid]);
  36. $node->left = $this->helper($nums, $left, $mid-1);
  37. $node->right = $this->helper($nums, $mid+1, $right);
  38. return $node;
  39. }
  40. function sortedListToBST1($head) {
  41. if($head == null) return null;
  42. $mid = $this->getMidNode($head);
  43. $node = new TreeNode($mid->val);
  44. if($mid == $head){
  45. return $node;
  46. }
  47. $node->left = $this->sortedListToBST($head);
  48. $node->right = $this->sortedListToBST($mid->next);
  49. return $node;
  50. }
  51. function getMidNode(&$head){
  52. $prePtr = null;
  53. $slowPtr = $head;
  54. $fastPtr = $head;
  55. while($fastPtr!=null && $fastPtr->next!=null){
  56. $prePtr = $slowPtr;
  57. $slowPtr = $slowPtr->next;
  58. $fastPtr = $fastPtr->next->next;
  59. }
  60. if($prePtr!=null){
  61. $prePtr->next = null;
  62. }
  63. return $slowPtr;
  64. }
  65. function helpers($head, $tail) {
  66. if ($head == null || $head == $tail) {
  67. return null;
  68. }
  69. $slow = $head;
  70. $fast = $head;
  71. while ($fast->next != $tail && $fast->next->next != $tail) {
  72. $fast = $fast->next->next;
  73. $slow = $slow->next;
  74. }
  75. $current = new TreeNode($slow->val);
  76. $current->left = $this->helper($head, $slow);
  77. $current->right = $this->helper($slow->next, $tail);
  78. return $current;
  79. }
  80. }

GO代码实现:

  1. /**
  2. * Definition for singly-linked list.
  3. * type ListNode struct {
  4. * Val int
  5. * Next *ListNode
  6. * }
  7. */
  8. /**
  9. * Definition for a binary tree node.
  10. * type TreeNode struct {
  11. * Val int
  12. * Left *TreeNode
  13. * Right *TreeNode
  14. * }
  15. */
  16. func sortedListToBST(head *ListNode) *TreeNode {
  17. nums, p := []int{}, head
  18. for p != nil {
  19. nums, p = append(nums, p.Val), p.Next
  20. }
  21. return helper(nums, 0, len(nums)-1)
  22. }
  23. func helper(nums []int, l, r int) *TreeNode {
  24. if l > r {
  25. return nil
  26. }
  27. mid := (l + r) / 2
  28. root := &TreeNode{Val: nums[mid]}
  29. root.Left, root.Right = helper(nums, l, mid-1), helper(nums, mid+1, r)
  30. return root
  31. }
  1. func sortedListToBST(head *ListNode) *TreeNode {
  2. return buildtree(head, nil)
  3. }
  4. func buildtree(head, tail *ListNode) *TreeNode {
  5. if head == tail {
  6. return nil
  7. }
  8. slow, fast := head, head
  9. for fast != tail && fast.Next != tail {
  10. slow = slow.Next
  11. fast = fast.Next.Next
  12. }
  13. root := &TreeNode{Val: slow.Val}
  14. root.Left = buildtree(head, slow)
  15. root.Right = buildtree(slow.Next, tail)
  16. return root
  17. }