[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

看一下node类的定义,有的不是val+next结构的,而是val+left+right结构的。

[思维问题]:

以为要用bst走一遍以后再翻,没想到bst里面可以直接在处理中间节点时翻(指定值迭代、指定指针)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

dummy节点真的是完全不起作用的,我们返回的是dummy.next

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

涉及到linkedlist遍历的题目都要用两个指针:cur和prev

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

bst里面可以直接在处理中间节点时翻(指定值迭代、指定指针)

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class TreeNode {
int val;
TreeNode left, right; TreeNode(int item) {
val = item;
left = right = null;
}
} class Solution {
Node prev = null; public Node treeToDoublyList(Node root) {
//corner case
if (root == null) return null; //initialization: dummy node
Node dummy = new Node(0, null, null);
dummy.right = prev;
prev = dummy; //uitlize the function
inOrderTraversal(root); //connect the first and last
prev.right = dummy.right;
dummy.right.left = prev; //return
return dummy.right;
} public void inOrderTraversal(Node cur) {
//exit case
if (cur == null) return ; //traverse l, change cur & prev, r
inOrderTraversal(cur.left);
cur.left = prev;
prev.right = cur;
prev = cur;
inOrderTraversal(cur.right);
}
}

426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表的更多相关文章

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

    原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...

  2. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  3. [leetcode]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 ...

  4. [LC] 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 ...

  5. [LeetCode] 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 ...

  6. LeetCode426.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 point ...

  7. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  8. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  9. Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

    (http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...

随机推荐

  1. java_初始化器

    1. 执行的顺序 package java20180129_1; public class Demo { // instance variable initializer 实例变量初始化器 Strin ...

  2. MyTomcat(手写服务器)

    Tomcat 是非常流行的 Web Server,它还是一个满足 Servlet 规范的容器.那么想一想,Tomcat 和我们的 Web 应用是什么关系? 从感性上来说,我们一般需要把 Web 应用打 ...

  3. bootstrap弹出模态框会给body加padding的解决方法

    bootstrap弹出模态框会给body加padding,导致页面缩放的解决方法: 在页面或是css文件里加上($paddingSize为less变量,需要改成像素或是其他单位,如12px,1rem) ...

  4. c# 数据存储过程

    什么是存储过程? 用某百科的话来说就是一堆为了完成某一功能或者某些功能的SQL语句的集合,而数据库则会将这些存储过程的方法存储到数据库中去. 优点: 1.可重用并且效率高:存储过程经过一次编译后不需要 ...

  5. xshell 显示中文

    xshell 可能无法正常显示中文,即使选择了utf-8编码也不生效. 1:输入:echo $LANG 显示:en_US.UTF-8 2.输入:export LANG=zh_CN.UTF-8 3. 输 ...

  6. 理解OpenShift(2):网络之 DNS(域名服务)

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  7. python:数据类型list

    一.列表list list是python中基础的数据类型之一,它是以[ ]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型 li = ['alex', 123, True, (1, 2, 3 ...

  8. aspose.cells 插入图片

    ,,"d:\\1.jpg"); Aspose.Cells.Drawing.Picture pic = worksheet.Pictures[iIndex]; pic.Placeme ...

  9. [SQL]某数据库中查出包含 字段名 的所有表名

    --利用SQL语句来查询字段所在的表 --从某数据库中查出包含 字段名 字段的所有表名 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE ...

  10. 教你phpstudy如何搭建本地多站点

    经常做多个网站同时开发,如何才能在本地能使部署多个站点,今天就来分享一下如何用PHPstudy搭建本地多站点. 点击上图中的 其它选项菜单 ,就会弹出下面的对话框,然后点击 站点域名管理 然后在 网站 ...