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)或递归 ...
随机推荐
- 将应用图标添加到ubuntu dash中
1 在appplications中添加一个desktop文件 sudo gedit /usr/share/applications/xdbe.desktop 2 在desktop文件中添加如下 [De ...
- 如何实现Nginx+Keepalived中Nginx进程的高可用
此架构我简单说明下: 一般为了维护方便,企业网站的服务器都在自己的内部机房里,只开放了Keepalived的VIP地址的两个端口80.443,通过Juniper SSG550防火墙映射出去,外网DNS ...
- pat1088. Rational Arithmetic (20)
1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue F ...
- 在C#.net中如何操作XML
在C#.net中如何操作XML 需要添加的命名空间: using System.Xml; 定义几个公共对象: XmlDocument xmldoc ; XmlNode xmlnode ; XmlEle ...
- web子项目的路径问题
http://baidu.com/userms/ userms是一个子应用程序,项目中使用的路径 /content/css/.. 从http://baidu.com/ 开始 ~/content/ ...
- ToDictionary写法
把List集合转化成Dictionary public ActionResult Dimo() { Dictionary<string, Object> param = new Dicti ...
- HttpClient4.3.3 禁止自动重定向
HttpClient4.3中默认允许自动重定向,导致程序中不能跟踪跳转情况,其实只需要在RequestConfig中setRedirectsEnabled(false)即可(默认是true): pri ...
- Windbg工具使用
https://www.cnblogs.com/startpoint/p/4194052.html https://www.cnblogs.com/lyl6796910/p/7613664.html ...
- laravel安装时openssl_encrypt() 的问题?Call to undefined function openssl_decrypt()
解决方案: 如果通过上面的步骤还是不能解决参考如下: 1.从php安装根目录中拷贝 libeay32.dll 和 ssleay32.dll 然后 覆盖掉apache/bin 下的对应文件(注意需要将h ...
- JSON FILE NOT FOUND?
(WIN7+IIS7) 搞一个小测试,一个小程序,用一个JSON文件来显示数据, 用HTML 访问是完全没有问题的,可是一放到IIS下,就出现无法访问, CONSOLE:提示 NOT FOUND 原 ...