BST转换成有序链表
把二元查找树转变成排序的双向链表(树)
题目:
输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。
要求不能创建任何新的结点,只调整指针的指向。
struct BSTreeNode{
int val;
BSTreeNode* left;
BSTreeNode* right;
BSTreeNode(int x) : val(x),left(nullptr),right(nullptr){}
};
void helper(BSTreeNode* root,BSTreeNode*& head, BSTreeNode*& tail){
if(!root) return;
helper(root->left,head,tail);
root->left = tail;
if(tail){
tail->right = root;
}else{
head = root;
}
tail = root;
helper(root->right,head,tail);
}
BSTreeNode* ConvertBSTree2List(BSTreeNode* root){
BSTreeNode* head = nullptr;//list head
BSTreeNode* tail = nullptr;//list tail
helper(root,head,tail);
return head;
}
int main(){
BSTreeNode* root = new BSTreeNode();
root->left = new BSTreeNode();
root->right = new BSTreeNode();
root->left->right = new BSTreeNode();
BSTreeNode* head;
head = ConvertBSTree2List(root);
return ;
}
BST转换成有序链表的更多相关文章
- LintCode-378.将二叉查找树转换成双链表
将二叉查找树转换成双链表 将一个二叉查找树按照中序遍历转换成双向链表. 样例 给定一个二叉查找树: 返回 1<->2<->3<->4<->5. 标签 链 ...
- lintcode:将二叉查找树转换成双链表
题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...
- 笔试算法题(31):将有序数组转换成BST表示 & 线段树的应用
出题:要求将一个有序整数数组转换成最小深度的Binary Search Tree表示: 分析:由于需要是最小深度,所以BST应保持平衡,左右节点数大致相当,并且BST中当前根节点大于所有其左子树中的元 ...
- convert sorted list to binary search tree(将有序链表转成平衡二叉搜索树)
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
[109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...
- [Swift]LeetCode109. 有序链表转换二叉搜索树 | Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode系列]有序链表转换为平衡BST的递归解法
给定有序链表(元素由小到大), 试问如何将其转换为一个平衡BST? 平衡BST: 任意节点的左右子树的深度差值不大于1. 主要思想是用递归. Trick是使用快慢指针来获取中间节点. 获得中间节点后, ...
- LeetCode109. 有序链表转换二叉搜索树
109. 有序链表转换二叉搜索树 问题描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超 ...
- (转)为什么HashMap中链表长度超过8会转换成红黑树
原博地址:https://blog.csdn.net/xingfei_work/article/details/79637878 HashMap在jdk1.8之后引入了红黑树的概念,表示若桶中链表元素 ...
随机推荐
- python面试题之如何计算一个字符串的长度
在我们想计算长度的字符串上调用函数len()即可 >>> len('hhhhhhhhjg') 10 所属网站分类: 面试经典 > python 作者:外星人入侵 链接:http ...
- 【转】Android EventBus初探
出处:http://blog.csdn.net/lmj623565791/article/details/40794879 1.概述 最近大家面试说经常被问到EventBus,github上果断dow ...
- 经典的兔子生兔子问题(C#递归解法)
古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 思路:先求出每个月新增的兔子,再用循环求和即可算出这个月 ...
- Privacy Policy of ColorfulBroswer
Personal information collection this app does not collect your data and does not share your infomat ...
- Mycat SqlServer Do not have slave connection to use, use master connection instead
Do not have slave connection to use, use master connection instead 很奇怪啊 都是按照配置配置的 怎么就是不通呢 有点怀疑人生了吧 其 ...
- Python3.5 学习三
对文件的操作 打开模式: 1 f=open("xxx","r",encoding=="utf-8") 只读 2 f=open("x ...
- python 杂谈
python 当前文件导入自定义模块的时候,会默认执行一遍 python使用的变量必须是已经定义或者声明过的.
- jmeter5.1.1启动提示not able to find java executable or version的解决办法
安装jmeter5.1.1完成后,启动报错not able to find java executable or version,如下图所示 解决办法: 1.在环境变量PATH的最后添加如下内容:%S ...
- iOS 基础之NSArray数组去重
1.面试题 现在有一个数组arr1,它里面存储的字符串分别为@“zhangsan”@“lisi”@“wangwu”@“lisi”@“zhangsan”,请将它去重后赋值给可变数组arr2输出为:@“z ...
- SaltStack 基础
介 SaltStack是基于Python开发的一套C/S架构配置管理工具.它的底层使用ZeroMQ消息队列pub/sub方式通信,使用SSL证书签发的方式进行认证管理.号称世界上最快的消息队列Zero ...