Convert Sorted double linked List to Binary Search Tree

Convert Sorted double linked  List to Binary Search Tree
In place

闫老师讲的这两个 答案 是把他 看成 single linked list了, 但是对于double linked list , 也适用

base case : at least three nodes , other wise mid.next and mid.next.next NPE

NOT TESTED YET 

小班文档

Solution one:

先处理 head.next =null
And head.next.next = null

闫老师也不是一上来就知道怎么做, 也是从 induction 做, 把general 的 case 处理掉
然后再回头看 base case ,分别从 size = 0, 1,2, 3 看起, 有没有越界, 之类的

是一步步分析出来的。 

现在做tag和 面经题, 也是要自己一步步分析出来, 面试的时候 靠的是之前准备时的功底, 不是
当场现想,或者无脑背答案

// one is list node . Another one Is tree node 

Draw the recursion tree
N  find the middle node on each level in total. There is logn levels
  So   O(NLOGN) in total 

class Solution {
    public TreeNode ddlToBST(ListNode head) {
      // base case
      if(head == null) return null;
      if(head.next == null) return ..;
      if(head.next.next == null) return ..;

      // recursion + induction rule
      ListNode mid = findMiddle(head);
      TreeNode root = new TreeNode(mid.next.val);
      ListNode next = mid.next.next;
      mid.next.next = null;
      mid.next = null;
      root.left = ddlToBST(head);
      root.right = ddlToBST(next);
      return root;
    }
}

Solution two:
一边构建 left subtree 一边对这个linked list 进行
遍历

time: o(n)

Convert Sorted double linked  List to Binary Search Tree
In place
还是不理解这个做法

Solution two:
一边构建 left subtree 一边对这个linked list 进行
遍历

time: O(n)

// get the length of the list
public TreeNode (ListNode head){
  int length = length(head);

  // construct the tree recursively
  return construct(0, length - 1);
}

ListNode curInOrder = head;
private TreeNode construct(int start, int end){
  // base case
  if(start > end) return null;

  int mid = start + (end - start) / 2;
  TreeNode left = construct(start, mid - 1);
  TreeNode root = new TreeNode(curInOrder.val);

  curInOrder = curInOrder.next;
  root.left = left;

  root.right = construct(mid + 1, end);
  return root;

}

private int length(ListNode head){
  int count = 0;
  ListNode pointer = head;
  while(pointer != null){
    count +=1;
    pointer = pointer.next;
  }
  return count;
}

///////
自己写的 solution 1 

// later I can test my code using this node class, and treat tree node and list node as the same
class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
}

public TreeNode dllToBST(ListNode head){
  // base case. when the size of the linked list is 0, 1, 2
  // when the size is 0
  if(head == null){
    return null;
  }

  // when the size is 1
  if(head.next = null){
    return new TreeNode(head.val);
  }

  // when the size of the linked list is 2
  if(head.next.next = null){
    TreeNode next = new TreeNode(head.next.val);
    TreeNode root = new TreeNode(head.val);
    root.right = next;
    return root;
  }

  // recursion + induction
  ListNode middle = findMid(head);
  TreeNode root = new TreeNode(middle.val);
  ListNode next = middle.next;
  ListNode prev = middle.prev;

  middle.next = null;
  prev.next = null;

  root.left = dllToBST(head);
  root.right = dllToBST(next);

  return root;
}

private ListNode findMid(ListNode head){
  // base case : 0 1 2 3
  // size 0, 1, 2 wont be here because they have taken care of
  // in the base case of the above func
  ListNode tail = head.prev;

  ListNode fast = head;
  ListNode slow = head;

  while(fast != tail && fast.next != tail){
    fast = fast.next.next;
    slow = slow.next;
  }

  return slow;
}

原文地址:https://www.cnblogs.com/tobeabetterpig/p/9490865.html

时间: 2024-10-16 03:54:14

Convert Sorted double linked List to Binary Search Tree的相关文章

LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 2.Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这里两道题目.是连在一起的两题.给你一个排好序(升序)的数组或者链表,将它们

426. Convert Binary Search Tree to Sorted Doubly Linked List

426. Convert Binary Search Tree to Sorted Doubly Linked List https://www.youtube.com/watch?v=FsxTX7-yhOw&t=1210s https://docs.google.com/document/d/1IIn5rXrUumqpxRrMKo76FbBx1ibTBDGso5rfENmkabw/edit class Solution { public Node treeToDoublyList(Node r

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

leetcode dfs Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree Total Accepted: 21420 Total Submissions: 78476My Submissions Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:把有序单链表转换为二叉查找树 思路: 用链表中心作为当作二叉树的根,

[LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元

leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 代码:oj测试通过 Runtime: 178 ms 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self

109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST 这题和convert array t

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr