zoukankan      html  css  js  c++  java
  • [LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List

    Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.

    You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. 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.

    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. You should return the pointer to the smallest element of the linked list.

    Example 1:

    Input: root = [4,2,5,1,3]
    
    

    Output: [1,2,3,4,5]
    
    Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor relationship.
    

    Example 2:

    Input: root = [2,1,3]
    Output: [1,2,3]
    

    Example 3:

    Input: root = []
    Output: []
    Explanation: Input is an empty tree. Output is also an empty Linked List.
    

    Example 4:

    Input: root = [1]
    Output: [1]

    Constraints:

    • -1000 <= Node.val <= 1000
    • Node.left.val < Node.val < Node.right.val
    • All values of Node.val are unique.
    • 0 <= Number of Nodes <= 2000

    将二叉搜索树转化为排序的双向链表。

    题意是将一个二叉搜索树就地转化为一个已排序的双向循环链表,要求是不能使用额外空间。那么思路就只能是递归的中序遍历。首先我们创建两个dummy节点,一个代表图示中的head节点,一个用来记录中序遍历中的前一个节点prev。在helper函数中,因为中序遍历的结果是有序的,所以当我们遍历到当前节点的时候,需要

    • prev.right = cur,把前一个节点的右指针指向当前节点
    • cur.left = prev,把当前节点的左指针指向prev节点
    • prev = root,把root记录到prev

    我们跑完整个树之后,记得把prev.next指向head,把head.left指向prev

    时间O(n)

    空间O(n) - 只用了递归的栈空间

    Java实现

     1 /*
     2 // Definition for a Node.
     3 class Node {
     4     public int val;
     5     public Node left;
     6     public Node right;
     7 
     8     public Node() {}
     9 
    10     public Node(int _val) {
    11         val = _val;
    12     }
    13 
    14     public Node(int _val,Node _left,Node _right) {
    15         val = _val;
    16         left = _left;
    17         right = _right;
    18     }
    19 };
    20 */
    21 
    22 class Solution {
    23     private Node dummy;
    24     private Node prev;
    25 
    26     public Node treeToDoublyList(Node root) {
    27         dummy = new Node(0, null, null);
    28         prev = dummy;
    29         // corner case
    30         if (root == null) {
    31             return null;
    32         }
    33 
    34         // normal case
    35         helper(root);
    36         prev.right = dummy.right;
    37         dummy.right.left = prev;
    38         return dummy.right;
    39     }
    40 
    41     private void helper(Node root) {
    42         if (root == null) {
    43             return;
    44         }
    45         helper(root.left);
    46         prev.right = root;
    47         root.left = prev;
    48         prev = root;
    49         helper(root.right);
    50     }
    51 }

    LeetCode 题目总结

  • 相关阅读:
    docker学习笔记(2)——docker常用命令
    docker学习笔记(1)——ubuntu16.04安装docker(含如何彻底卸载docker,docker拉取镜像失败解决)
    近期计划随笔
    程序员的自我修养:链接、装载与库(一本书,估计没时间看。。。哭。。。)
    str.format 学习
    pyqt5 线程 (QThread)启动、挂起、恢复、终止问题见解
    pyqt5 在非主线程(QThread线程)中实现弹窗QMessageBox
    807 · 回文数 II
    1334 · 旋转数组
    297 · 寻找最大值
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14077476.html
Copyright © 2011-2022 走看看