[抄题]:

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. 全志A33 lichee怎样编译镜像

    对于全志A33 lichee编译镜像文件需要先搭建好交叉编译环境,这个搭建环境可以看之前的文档 "SINA33开发板怎样创建编译环境" 开发平台 * 芯灵思SinlinxA33开发 ...

  2. 什么是pytorch(3神经网络)(翻译)

    神经网络 torch.nn 包可以用来构建神经网络. 前面介绍了 autograd包, nn 依赖于 autograd 用于定义和求导模型. nn.Module 包括layers(神经网络层), 以及 ...

  3. golang-grpc-Unimplemented-desc

    golang 调用grpc 服务方法时候提示:"rpc error: code = Unimplemented desc ="的错误, 这是由于pb中的package name 被 ...

  4. python(二)——list、字典、字符串操作

    列表——list 假设一种场景,想要存班里所有同学的名字,那应该拿什么存呢? 我们可以拿一个字符串去存,例如: stus = "s1,s2,s3,s4,s5……sn" 那我们要从里 ...

  5. CentOS6.8 配置LVM

    LVM是逻辑盘卷管理(Logical Volume Manager)的简称,Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性. LVM ...

  6. py-day3 python 全局变量和局部变量

    # 全局变量 如果函数的内容无 global关键字,优先读取全局变量,无法对全局变量重新赋值, name = 'mj' def change_name(): print('change_name',n ...

  7. SQL 语句 explain 分析

      分析索引的效率: > EXPLAIN sql; EXPLAIN 分析的结果的表头如下: id | select_type | table | partitions | type | poss ...

  8. Linux查看线程

    我的程序在其内部创建并执行了多个线程,我怎样才能在该程序创建线程后监控其中单个线程?我想要看到带有它们名称的单个线程详细情况(如,CPU/内存使用率). 线程是现代操作系统上进行并行执行的一个流行的编 ...

  9. USB3.0及NVME SSD安装WIN7X64

    USB3.0及NVME SSD安装WIN7X64https://tieba.baidu.com/p/4822034273?pn=1所有的人都是菜鸟过来的,不过有些人懂得自己动手找到答案:有些人则是懒得 ...

  10. inception 自动化sql审核

    ##概念: Inception是一款自动化运维的利器,有别与现在各个公司使用的方式,使用Inception,将会给DBA带来最大的便利性,将DBA从繁冗的工作中解放出来,做一些更多的自动化工作,或者从 ...