426. Convert Binary Search Tree to Sorted Doubly Linked List

Posted tobeabetterpig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了426. Convert Binary Search Tree to Sorted Doubly Linked List相关的知识,希望对你有一定的参考价值。

426. Convert Binary Search Tree to Sorted Doubly Linked List(AC)
Convert a BST to a sorted circular doubly-linked list in-place


Iterative(AC)
O(n) time, O(h) space. Stack size depends on the  , tree’s height 

/*
// Definition for a Node.
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;
    }
};
*/

class Solution {
    public Node treeToDoublyList(Node root) {
      if(root == null) return null;
      Stack<Node> stack = new Stack<>();
      if(root != null){
        pushLeft(root, stack);
      }
      
      Node first = stack.peek();
      
      while(!stack.isEmpty()){
        Node cur = stack.pop();
        
        if(cur.right != null){
          pushLeft(cur.right, stack);
        }
        
        if(!stack.isEmpty()){ // check if empty
          
          Node peek = stack.peek();
          cur.right = peek;
          peek.left = cur;
        }
        
        if(stack.isEmpty()){
          cur.right = first;
          first.left = cur;
        }
      }
      return first;
    }
  
    private void pushLeft(Node node, Stack<Node> stack){
      while(node != null){ // use while 
        stack.push(node);
        node = node.left;
      }
    }
}

// 写过的题还会出错, 当时花了很长时间把这道题用 iterative 的方法
做了出来, 但是这次再写, 不会写了, 看了答案后, 自己写了一遍, 又
出现了错误。。。
建议: 走例子, 常复习。






// recursive 闫老师笔记 (AC)
O(n) time, O(h) space . Call stack  depends on the tree’s height 

class Solution {
    public Node treeToDoublyList(Node root) {
      if(root == null) return null;
      
      // suppose both left and right are circuler ddl already sepatrely
      // each of them is in their own ddl bubble
      // they are not connected yet 
      Node left = treeToDoublyList(root.left);
      Node right = treeToDoublyList(root.right);
      
      
      // make the root connected with itself, then root is 
      // also a circuler ddl
      root.left = root;
      root.right = root;
      
      
      // connect the left circuler ddl with root circular ddl
      // return the new head, which is the head of left, if 
     // left is not null. if left is null, the new head is the root
      left = connect(left, root);
      
      // connect the new left with the right circular ddl
      // return the new head, which is the head of left, if
      // left is not null. return head of right if left is null
      // we just call the return value left, it‘s not necessaitly 
      // the head of the left , if all depends if the left is null
      // or not
      left = connect(left, right);
      
      return left;
        
    }
  
  
    // connect one circular ddl with another circular ddl 
    // 
    private Node connect(Node h1, Node h2){
      if(h1 == null) return h2;
      if(h2 == null) return h1;
      
      Node t1 = h1.left;
      Node t2 = h2.left;
      
      h1.left = t2;
      t2.right = h1;
      
      
      h2.left = t1;
      t1.right = h2;
      
      return h1;
    }
}

// suppose <-1<->2<->3->     1 is h1, 3 is t1. 1 and 3 is connected 
//         <-4<->5<->6->     4 is h2, 6 is t2. 4 and 6 is connected

// now we want to connect the above circular ddl. 
// we want to get    <-1<->2<->3<->4<->5<->6->
// then h1.prev = t2. t2.next = h1.    t1.next = h2. h2.prev = t1









/////
自己写的, 和闫老师的不太一样的一点是

      
      // connect the left circuler ddl with root circular ddl
      // return the new head, which is the head of left, if 
     // left is not null. if left is null, the new head is the root
      Node newHead = connect(left, root);
      
      // connect the new list with the right circular ddl
      // return the new head, which is the head of the new list, if
      // new list is not null. return head of right if the new list is null
      
      newHead= connect(newHead, right);
      
      return newHead;

因为 感觉 用个 newhead 比较 容易理解。 



class Solution {
    public Node treeToDoublyList(Node root) {
      if(root == null) return null;
      
      // suppose both left and right are circuler ddl already sepatrely
      // each of them is in their own ddl bubble
      // they are not connected yet 
      Node left = treeToDoublyList(root.left);
      Node right = treeToDoublyList(root.right);
      
      
      // make the root connected with itself, then root is 
      // also a circuler ddl
      root.left = root;
      root.right = root;
      
      
      // connect the left circuler ddl with root circular ddl
      // return the new head, which is the head of left, if 
     // left is not null. if left is null, the new head is the root
      Node newHead = connect(left, root);
      
      // connect the new list with the right circular ddl
      // return the new head, which is the head of the new list, if
      // new list is not null. return head of right if the new list is null
      
      newHead= connect(newHead, right);
      
      return newHead;
        
    }
  
  
    // connect one circular ddl with another circular ddl 
    // 
    private Node connect(Node h1, Node h2){
      if(h1 == null) return h2;
      if(h2 == null) return h1;
      
      Node t1 = h1.left;
      Node t2 = h2.left;
      
      h1.left = t2;
      t2.right = h1;
      
      
      h2.left = t1;
      t1.right = h2;
      
      return h1;
    }
}

 

以上是关于426. Convert Binary Search Tree to Sorted Doubly Linked List的主要内容,如果未能解决你的问题,请参考以下文章

426. Convert Binary Search Tree to Sorted Doubly Linked List - Medium

LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

[leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Searc

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

Convert Sorted Array to Binary Search Tree