题目

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.


Tag


代码

分治法。递归。pre引用节点。中序遍历。

/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(!pRootOfTree) return nullptr;
TreeNode* pre = nullptr;
Core(pRootOfTree,pre); while(pRootOfTree->left)
{
pRootOfTree = pRootOfTree->left;
}
return pRootOfTree;
}
//中序遍历。递归。
void Core(TreeNode* root,TreeNode*& pre)
{
if(!root) return;//终止 //左
Core(root->left,pre);
if(pre)
{
pre->right=root;
root->left=pre;
} //根
pre =root; //右
Core(root->right,pre);
}
};

问题

LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List的更多相关文章

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

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

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

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

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

  9. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

随机推荐

  1. [转]win系统下nodejs安装及环境配置

    本文转自:http://www.cnblogs.com/linjiqin/p/3765390.html 第一步:下载安装文件 下载nodejs,官网:http://nodejs.org/downloa ...

  2. oracle 11g 将非分区表转换为分区表在线重定义

    --操作的用户需要有以下的权限 GRANT CONNECT, RESOURCE TO CMIGDW; GRANT EXECUTE ON DBMS_REDEFINITION TO CMIGDW; GRA ...

  3. Linux平台命令挂载U盘——实现数据共享

    废话少说,一一道来,Linux中按照步骤来做就可以啦.(嵌入式平台) 1.先看看本地的mount信息 # mountrootfs on / type rootfs (rw)/dev/root on / ...

  4. Express中404页面

    404页面是各大网站都需要的. 在做express项目时,应当注意,404页面在app.js中的判断是在最后的,使用这个中间件时,不需要next(),因为它是最后一个了. 它之前一般是router. ...

  5. Quartz使用(5) - Quartz的Job存储及集群部署

    1. Job的存储与持久化 Quartz的JobStore接口定义了作业Job.触发器trigger.调度器Scheduler等数据存储机制.Quartz主要有两种Job存储类型:内存存储RAMJob ...

  6. 【蓝牙】蓝牙,调试 hcitool与gatttool实例

    Bluez协议栈在安装完以后,会提供两个命令行调试工具,hcitool与gattool,我们可以根据提供的工具来轻松的调试我们的蓝牙设备,调试BLE设备时,需要获取root权限. 蓝牙设备的开启与关闭 ...

  7. Newtonsoft.Json解析json字符串和写json字符串

    写: StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); //如果报错则使用JsonWriter ...

  8. AutoResetEvent 2

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. Hibernate课程 初探一对多映射4-3 测试--信息查询

    建立双向一对多关系,既可以由一方查询多方信息,同样可以由多方查询一方信息 demo: //查询学生所在班级 public static void showGidByStudent(){ Session ...

  10. PHP迭代器 Iterator

    Iterator是PHP自带的迭代器接口. 实现该接口的类必须实现该接口的方法,以便能够使用foreach进行输出迭代后的数据. interface Iterator extends Traversa ...