题目

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. mongodb3集群搭建

    三台服务器:先设置hosts 10.0.0.231 node1 10.0.0.232 node2 10.0.0.233 node3 1:下载 mongodb-linux-x86_64-rhel70-3 ...

  2. 在Java中VO , PO , BO , QO, DAO ,POJO是什么意思

    在Java中VO , PO , BO, DAO ,POJO是什么意思 最近在项目中,遇到VO,我的天...那就一起学习回忆一下 首先简单说明下: O/R  Mapping是Object Relatio ...

  3. 先定个小目标, 使用C# 开发的千万级应用

    dotNET跨平台 微信号 opendotnet 功能介绍 在这里你可以谈微软.NET,Mono的跨平台开发技术,也可以谈谈其他的跨平台技术.在这里可以让你的.NET项目有新的思路,不局限于微软的技术 ...

  4. autofac 一个接口多个实现的顺序执行

    接口: namespace AutofacTest.Interface { public interface IUserInfo { string GetUserINfo(int uid); int ...

  5. maven课程 项目管理利器-maven 3-10 maven聚合和继承 4星

    本节主要讲了以下内容: 1 maven聚合 2 maven继承 1 maven聚合 <!-- 聚合特有标签 --> <groupId>com.hongxing</grou ...

  6. maven课程 项目管理利器-maven 3-7 maven依赖范围 2星

    本节主要讲了maven的依赖范围: 在pom.xml   dependency标签的scope中.eclipse中有编译的路径,maven中有编译,运行,测试的路径. 1 scope为test,为测试 ...

  7. html 01前沿-web介绍

    1. 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 2. 浏览器(显示代码) 浏览器是网页显示.运行的平台,常用的浏览器有IE.火 ...

  8. Android无需权限显示悬浮窗

    TYPE_TOAST一直都可以显示, 但是用TYPE_TOAST显示出来的在2.3上无法接收点击事件, 因此还是无法随意使用. 下面是我之前研究后台线程显示对话框的时候记得笔记, 大家可以看看我们项目 ...

  9. jquery.dad.js实现table的垂直拖拽(并取到当前拖拽对象)

    http://sc.chinaz.com/jiaoben/161202572210.htm 1.首先官网实例,实现的都是div为容器的元素拖拽,示例如下: 2.最近的项目,要实现tbody的每一行tr ...

  10. Linux文件属性与权限

    一.在Linux里面,任何一个文件都具有“User,Group,Others”(用户.用户组.其他人)三种身份 二.用户组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个用户组的 ...