码迷,mamicode.com
首页 > 其他好文 > 详细

426. Convert Binary Search Tree to Sorted Doubly Linked List

时间:2018-08-17 00:41:04      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:错误   init   head   public   The   class   ready   def   efi   

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

标签:错误   init   head   public   The   class   ready   def   efi   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!