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.

 
Solution 1:
Make use of dummy node for temporarily prev head for doubly linked list
"""
# Definition for a Node.
class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right
"""
class Solution:
def treeToDoublyList(self, root: 'Node') -> 'Node':
if root is None:
return
dummy = Node(-1, None, None)
self.prev = dummy
self.helper(root)
self.prev.right = dummy.right
dummy.right.left = self.prev
return dummy.right def helper(self, cur):
if cur is None:
return
self.helper(cur.left)
self.prev.right = cur
cur.left = self.prev
self.prev = cur
self.helper(cur.right)
 
Solution 2:
Use head to record the real head when find it
"""
# Definition for a Node.
class Node:
def __init__(self, val, left, right):
self.val = val
self.left = left
self.right = right
"""
class Solution:
def treeToDoublyList(self, root: 'Node') -> 'Node':
if root is None:
return
self.prev, self.head = None, None
self.helper(root)
self.prev.right = self.head
self.head.left = self.prev
return self.head def helper(self, cur):
if cur is None:
return
self.helper(cur.left)
if self.prev is None:
self.head = cur
else:
self.prev.right = cur
cur.left = self.prev
self.prev = cur
self.helper(cur.right)
 

[LC] 426. Convert Binary Search Tree to Sorted Doubly Linked List的更多相关文章

  1. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  2. 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 ...

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

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

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

  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. LC 98. Validate Binary Search Tree

    题目描述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  9. LC 272. Closest Binary Search Tree Value II 【lock,hard】

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

随机推荐

  1. c++ 排序 冒泡 插入 选择 快速

    //冒泡 #include <iostream> using namespace std; void bubbleSort(int* list,int index) { ;i--) //i ...

  2. tomcat8.5的安装、卸载、配置和部署

    安装和卸载 下载 http://tomcat.apache.org/ 环境变量 1.点击此电脑 右键—>属性. 2.创建变量名为CATALINA_HOME,的值为所解压安装tomcat的本地目录 ...

  3. SQL基础教程(第2版)第4章 数据更新:练习题

    行也选取不出来. >> 解答 A 先生使用 BEGIN TRANSACTION 启动了事务处理,然后开始执行 INSERT 语句.因此,在 A 先生使用 COMMIT 确定该更新之前, B ...

  4. CF round #622 (div2)

    CF Round 622 div2 A.简单模拟 B.数学 题意: 某人A参加一个比赛,共n人参加,有两轮,给定这两轮的名次x,y,总排名记为两轮排名和x+y,此值越小名次越前,并且对于与A同分者而言 ...

  5. Linxu--导出oracle数据

    配置导出的表:(configure.sh) #!/bin/bash function get_config() { tables=( BLOG WF_PROCESS WF_TASK WF_TASK_A ...

  6. Thread--volatile详细

  7. Lyft、Uber、滴滴涉足汽车租赁领域,能打破既有汽车所有权模式吗?

    自共享经济出现之后,众多相关项目遍地开花.这些共享经济项目对于人们来说,最直观的感受就是实惠.性价比高.方便.不过抛开这些使用层面的优点来看的话,共享经济项目最大的特色或许就是改变了事物的所有权.一件 ...

  8. 并发与高并发(八)-线程安全性-原子性-synchronized

    前言 闲暇时刻,谈一下曾经在多线程教程中接触的同步锁synchronized,相当于复习一遍吧. 主要介绍 synchronized:依赖JVM Lock:依赖特殊的CPU指令,代码实现,Reetra ...

  9. Python说文解字_半成品再加工

    1. 其实在编写代码的时候,根据需求和程序员的喜好,对现有类中的属性和方法进行二次加工,原先所给与的属性和方法贴合自己想要的需求.这就是我们常说的“重写”和二次封装. 2. 比如我们对现有的库list ...

  10. LeetCode——324. 摆动排序 II

    给定一个无序的数组 nums,将它重新排列成 nums[0] < nums[1] > nums[2] < nums[3]... 的顺序. 示例 1: 输入: nums = [1, 5 ...