【linked-java】426.Convert Binary Search Tree to Sorted Doubly Linked List

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.

Let’s take the following BST as an example, it may help you understand the problem better:
在这里插入图片描述
We want to transform this BST into a circular doubly linked list. Each node in a doubly linked list has a predecessor and successor. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.

The figure below shows the circular doubly linked list for the BST above. The “head” symbol means the node it points to is the smallest element of the linked list.

在这里插入图片描述
Specifically, we want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. We should return the pointer to the first element of the linked list.

The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
在这里插入图片描述题解:

The order is inorder traversal.

Use stack to help inorder traversal. When root is not null, push it into stack and move to its left child.

When root is null, stack is not empty, pop the top and append it to current node’s right. Then root = top.right.

When root is null, stack is empty, assign current node’s right to dummy.right, and dummy.right.left = current.

Time Complexity: O(n).

Space: O(h).

answer analysis

这道题给了一个二叉搜索树,让我们将其转化为双向链表。并且题目中给了一个带图的例子,帮助理解。题目本身并不难理解,仔细观察下给的示例图。首先,转化成双向链表的每个结点都有 left 和 right 指针指向左右两个结点,不管其原来是否是叶结点还是根结点,转换后统统没有区别。其次,这是个循环双向链表,即首尾结点是相连的,原先的二叉搜索树中的最左结点和最右结点,现在也互相连接起来了。最后,返回的结点不再是原二叉搜索树的根结点 root 了,而是最左结点,即最小值结点。

好,发现了上述规律后,来考虑如何破题。根据博主多年经验,跟二叉搜索树有关的题,肯定要利用其性质,即左<根<右,即左子结点值小于根结点值小于右子结点值。而且十有八九都得用中序遍历来解,因为中序遍历的顺序就是左根右啊,跟性质吻合。观察到原二叉搜索树中结点4连接着结点2和结点5,而在双向链表中,连接的是结点3和结点5,这就是为啥要用中序遍历了,因为只有中序遍历,结点3之后才会遍历到结点4,这时候可以将结点3和结点4串起来。决定了用中序遍历之后,就要考虑是迭代还是递归的写法,博主建议写递归的,一般写起来都比较简洁,而且递归是解树类问题的神器啊,十有八九都是用递归,一定要熟练掌握。再写中序遍历之前,其实还有难点,因为需要把相邻的结点连接起来,所以需要知道上一个遍历到的结点是什么,所以用一个变量 pre,来记录上一个遍历到的结点。还需要一个变量 head,来记录最左结点,这样的话,在递归函数中,先判空,之后对左子结点调用递归,这样会先一直递归到最左结点,此时如果 head 为空的话,说明当前就是最左结点,赋值给 head 和 pre,对于之后的遍历到的结点,那么可以和 pre 相互连接上,然后 pre 赋值为当前结点 node,再对右子结点调用递归即可,参见代码如下:
是C++的就不写的。。。
附链接

answer one

class Solution {
  // the smallest (first) and the largest (last) nodes
  Node first = null;
  Node last = null;

  public void helper(Node node) {
    if (node != null) {
      // left
      helper(node.left);
      // node 
      if (last != null) {
        // link the previous node (last)
        // with the current one (node)
        last.right = node;
        node.left = last;
      }
      else {
        // keep the smallest node
        // to close DLL later on
        first = node;
      }
      last = node;
      // right
      helper(node.right);
    }
  }

  public Node treeToDoublyList(Node root) {
    if (root == null) return null;

    helper(root);
    // close DLL
    last.right = first;
    first.left = last;
    return first;
  }
}

answer two

/*
// 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 root;
        }
        
        Stack<Node> stk = new Stack<Node>();
        Node dummy = new Node();
        Node cur = dummy;
        while(root!=null || !stk.isEmpty()){
            if(root != null){
                stk.push(root);
                root = root.left;
            }else{
                Node top = stk.pop();
                cur.right = top;
                top.left = cur;
                cur = cur.right;
                root = top.right;
            }
        }
        
        cur.right = dummy.right;
        dummy.right.left = cur;
        
        return dummy.right;
    }
}

类似Binary Tree Inorder Traversal.
参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【Solution】 To convert a binary search tree into a sorted circular doubly linked list, we can use the following steps: 1. Inorder traversal of the binary search tree to get the elements in sorted order. 2. Create a doubly linked list and add the elements from the inorder traversal to it. 3. Make the list circular by connecting the head and tail nodes. 4. Return the head node of the circular doubly linked list. Here's the Python code for the solution: ``` class Node: def __init__(self, val): self.val = val self.prev = None self.next = None def tree_to_doubly_list(root): if not root: return None stack = [] cur = root head = None prev = None while cur or stack: while cur: stack.append(cur) cur = cur.left cur = stack.pop() if not head: head = cur if prev: prev.right = cur cur.left = prev prev = cur cur = cur.right head.left = prev prev.right = head return head ``` To verify the accuracy of the code, we can use the following test cases: ``` # Test case 1 # Input: [4,2,5,1,3] # Output: # Binary search tree: # 4 # / \ # 2 5 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5 # Doubly linked list in reverse order: 5 <-> 4 <-> 3 <-> 2 <-> 1 root = Node(4) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) # Test case 2 # Input: [2,1,3] # Output: # Binary search tree: # 2 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 # Doubly linked list in reverse order: 3 <-> 2 <-> 1 root = Node(2) root.left = Node(1) root.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) ``` The output of the test cases should match the expected output as commented in the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值