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 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的更多相关文章
- [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 ...
- 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 ...
- 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二叉搜索树转有序双向链表
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...
- [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 ...
- 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 ...
- 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 ...
- 算法与数据结构基础 - 二叉查找树(Binary Search Tree)
二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...
随机推荐
- 性能测试工具LoadRunner05-LR之Virtual User Generator html模式与url模式
“HTML-based script”说明 在默认情况下,选择“HTML-based script”,说明脚本中采用HTML页面的形式来表示,这种方式的脚本容易维护,容易理解,推荐这种方式录制 “UR ...
- 【计算机网络】一步一步学习IP路由流程
TCP/IP协议簇是目前互联网应用最广的协议栈,谈到TCP/IP协议栈就不能不讲一讲IP路由的问题,因为在我们使用的网络通信中几乎每时每刻都在发生着IP路由的事件…….当你在网络世界中还是一位新手的时 ...
- DEDE列表页和内容页调用顶级栏目ID的方法
dede模板中添加顶级栏目id的方法总结,使用dede顶级栏目id可以实现很多功能.比如,在每个列表页调用不同的栏目图片(同一顶级栏目调用相同的图片),如果我们做N个栏目就意味着要做N个列表页模板,显 ...
- PHP高级分离术
<!--高级分离术--> <table border='1px'> <?php foreach($data as $key=>$value){ ...
- React.js 小书 Lesson2 - 前端组件化(一):从一个简单的例子讲起
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson2 转载请注明出处,保留原文链接和作者信息. 很多课程一上来就给大家如何配置环境.怎么写 Re ...
- Mysql的transaction实现(转)
(http://www.blogjava.net/i369/archive/2007/04/29/108906.html) transaction在数据库编程中是一个重要的概念,这样做可以控制对数据库 ...
- orientationchange事件
orientationchange事件 resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize':
- EF--Model First
Model First先设计Model对象,再由对象生成数据库. 1.新建控制台项目,名称ModelFirst,确定. 2.点击选中项目,右键-->添加-->新建项目--选择数据模板--& ...
- 【MATLAB】画信号频谱的子函数
输入信号序列和采样率,该子函数可以画出该信号的频谱图. function [f,spec,NFFT]=spec_fft_plot(sample,L,Fs) % 输入数据说明: % sample:信号序 ...
- django choice字段模板展示
class UserInfo(AbstractUserInfo): """ 用户表 """ gender_choice = ( (1,&qu ...


