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

"""
# 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)
"""
# 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的更多相关文章
- 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 ...
- 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 ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- slam库安装
Ceres安装: 1.Ceres是一个cmak工程,首先要安装他的依赖项,使用apt-get安装. sudo apt-get install liblapack-dev libsuitesparse- ...
- 106.HttpResponse对象详解
HttpResponse对象 Django服务器接收到客户端发送过来的请求之后,会将提交上来的这些数据封装成一个HttpResquest对象传给视图函数.那么视图函数在处理完成相关的逻辑后,也需要返回 ...
- delphpi tcp 服务和客户端 例子
//服务器端unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, ...
- Glob 模式
Glob 是什么 glob 是一种文件匹配模式,全称 global,它起源于 Unix 的 bash shell 中,比如在 linux 中常用的 mv *.txt tmp/ 中,*.txt 就使用到 ...
- Codeforce 370C Mittens 巧妙数学题
这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...
- Xpath编码问题解决
使用Xpath获取属性时,出现乱码问题,解决办法找了好多,终于解决,特将办法贴在这,供大家尝试 不要直接简单的将爬取的网页设置为utf-8, 先通过print(r.encoding)输出看看爬取的是什 ...
- 如何在C语言 C++里面调用 DOS命令
C里面调用可以用[system("命令")]这样的形式. 但需要include <stdlib.h> 例子如下: #include <stdio.h> #i ...
- Tensorflow学习教程------下载图像识别模型inceptionV3
# coding: utf-8 import tensorflow as tf import os import tarfile import requests #inception模型下载地址 in ...
- 10. 通过 Dockerfile 编写 linux 命令行工具
测试 linux 压力的工具 一. 实际操作 1. 创建一个 ubuntu 的容器 docker run -it ubuntu 2. 安装 stress 工具 apt-get update & ...
- 第一行代码近期bug及解决
Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法https://www.cnblogs.com/dongling/p/6476308.ht ...