`

Convert a BST to a sorted circular 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.


If the problem statement is still not clear to you, below is a pictorial representation of what you need to do:

 

i) an ordered binary tree (BST) storing numbers from 1 – 5.
ii) original tree converted to a sorted circular doubly-linked list. Its left and right pointers were modified to point to its previous and next node.

I originally read this interesting problem here: The Great Tree-List Recursion Problem.

Hint:
Think of in-order traversal. How do you ensure that the last element’s right pointer points back to the first element?

A circular doubly-linked list. Think of the left and right pointers in a tree as synonymous to the previous and next pointers in a list.

Solution: 
When I first see this problem, my first thought was in-order traversal. Couldn’t we modify the nodes’ left and right pointers as we do an in-order traversal of the tree? However, we have to beware not to modify the pointers and accessing it at a later time.

As we traverse the tree in-order, we could safely modify a node’s left pointer to point to the previously traversed node as we never use it once we reach a node. We would also need to modify the previously traversed node’s right pointer to point to the current node. Note: The previously traversed node meant here is not its parent node. It is the node’s previous smaller element.

Easy approach, right? But wait, we are still missing two more steps. First, we did not assign the list’s head pointer. Second, the last element’s right pointer does not point to the first element (similar to the first element’s left pointer).

How do we solve this? My approach is pretty easy: Just update the current node’s right pointer to point back to the head and the head’s left pointer to point to current node in each recursive call. As the recursion ends, the list’s head and tail would be automagically updated with the correct pointers. Don’t forget to check for this special case: A list with only one element should have its left and right pointers both pointing back to itself.

A double-linked list with a length of one.

Do you think this approach works? I bet it did! The run time complexity for this solution is O(N) since we are essentially doing a modified in-order traversal. It does have some extra assignments in each recursive call though. But overall I am quite satisfied with this approach because it is intuitive and easy to follow. Besides, we are adapting an existing algorithm (in-order traversal) to solve this problem, isn’t this just neat?

private TreeNode prev;
private TreeNode head;
public TreeNode bstToSortedDLL(TreeNode node) {
	if(node == null) return null;
	bstToSortedDLL(node.left);
	node.left = prev;
	if(prev != null) {
		prev.right = node;
	} else {
		head = node;
	}
	prev = node;
	TreeNode right = node.right;
	head.left = node;
	node.right = head;
	bstToSortedDLL(right);
	return head;
}

 

Reference:

http://articles.leetcode.com/2010/11/convert-binary-search-tree-bst-to.html

分享到:
评论

相关推荐

    BST-BMP280-DS001-12.pdf

    《BMP280数字压力传感器数据手册》是Bosch Sensortec公司发布的一份技术参考资料,文档编号BST-BMP280-DS001-12,修订版本为1.15,发布日期为2015年10月15日。该手册详细介绍了BMP280这款数字压力传感器的关键参数和...

    BST-BMP180-DS000-07.pdf

    4. **低功耗**:在标准模式下,每秒采样一次时电流消耗仅5μA。 5. **低噪声**: - 在超低功耗模式下,噪声为0.06 hPa (0.5m)。 - 在超高分辨率模式下,噪声可低至0.02 hPa (0.17m)。 6. **温度测量**:内置温度...

    BST-BME280_DS001-10.pdf

    The BME280 supports a full suite of operating modes which provides the flexibility to optimize the device for power consumption, resolution and filter performance." Applications - Context awareness,...

    BST-BMP180-DS000-08.zip_BMP180_bmp180 a_bmp180pdf_bst-bmp180_气体传

    BST-BMP180-DS000-08.zip 文件包含了有关 BMP180 气体传感器的详细信息,这款传感器由 BOSCH 公司生产,主要用于环境温度和气压的精确测量。BMP180 是一款集成度高、性能稳定的传感器,广泛应用于气象站、智能家居、...

    BST-BMP280-DS001-18手册.pdf

    BST-BMP280-DS001-18手册.pdf 本文档是关于BMP280数字压力传感器的数据手册,发布日期为2016年11月2日,文档编号为BST-BMP280-DS001-18。以下是从该文档中提取的知识点: 一、BMP280数字压力传感器的概述 * BMP...

    GBT7714-2005NLang.bst样式文件

    《GBT7714-2005NLang.bst样式文件详解与应用》 在学术研究和出版领域,正确引用参考文献是至关重要的。中国国家标准GB/T 7714-2005《文后参考文献著录规则》为中文文献的引用提供了统一规范。其中,`GBT7714-2005...

    GBT7714-2005NLang_Upp.bst GBT7714-2005NLang_Up.bst GBT7714-2005NLang.bst

    将bst文件放到MiKTeX的bst文件夹下自己新建的gbt7714-2005文件下,1)作者名称如 KERNER B S 的样式 GBT7714-2005NLang.bst 2)作者名称如 KERNER B S 的样式 GBT7714-2005NLang_UP.bst 3)作者名称如 Kerner B S 的...

    BST-BMP280-DS001-11

    在1Hz采样率下,电流消耗为2.7微安(µA)。 在温度性能方面,BMP280的温度范围覆盖-40至+85摄氏度,能够适应各种极端环境。此外,该传感器满足RoHS指令的要求,不含有害物质,符合环保标准。BMP280的设计使其具有1...

    bst-4wd+V4.0发行版1

    根据提供的信息,我们可以了解到这是一份关于bst-4wd+V4.0发行版1的文档,主要关注的是与stm32微控制器相关的硬件设计细节。由于提供的文档内容较为复杂且部分信息不易解读,以下将重点围绕标题、描述以及部分可见...

    5、步骤5 相关例程.zip_51单片机_BST-V51 51单片机_BST-V51程序_iic数码管

    在本压缩包中,我们聚焦于51单片机编程,特别是BST-V51型号的51单片机。这个集合包含了大量的实例代码,旨在帮助开发者深入理解和掌握51单片机的使用,以及与其相关的IIC接口和数码管显示技术。 首先,"51代码"是指...

    BST-BMP280-DS001-11.zip_BMP280_BMP280 data sheet_This Is It

    **BST-BMP280-DS001-11.zip** 包含的是BOSCH Sensortec公司的BMP280传感器的数据手册。**BMP280** 是一款高度集成的数字压力和温度传感器,广泛应用于气象、环境监测、物联网设备以及消费电子产品中。这份数据手册是...

    BST-BMI160-DS000-1509569.pdf

    3. **兼容Android Lollipop系统**:支持显著运动检测及步数计数器功能,每项功能仅消耗5μA的电流。 4. **小型化设计**:尺寸仅为2.5×3.0毫米²,厚度为0.83毫米,非常适合空间有限的应用场合。 5. **内置电源管理...

    bst-bmi088-ds001.pdf

    BMI088是一款六轴运动追踪的惯性测量单元(IMU),能够检测六个自由度(6DoF)内的移动和旋转。它集成了两个惯性传感器的功能于一个设备中:一个先进的三轴16位陀螺仪和一个多功能的、领先的三轴16位加速度计。...

    BST-BME680-DS001-1509608.pdf

    根据提供的文档内容,我们可以了解到关于BME680传感器的详细知识点。BME680是由博世传感科技(Bosch Sensortec)开发的一款低功耗数字4合1环境传感器,具备测量气体、湿度、气压和温度的功能。以下是详细知识点: ...

    BST-V51智能小车底板原理图1

    ### BST-V51智能小车底板电路原理图 #### 一、整体概述 BST-V51智能小车是一款集成了多种传感器与执行器的智能设备,主要用于教育及科研领域。其底板电路原理图展示了该智能小车的核心硬件设计,包括舵机供电模块、...

    Latex修改参考文献展示方式:修改apsrev4-1.bst文件

    Latex修改参考文献展示方式:修改apsrev4-1.bst文件 Latex是一种基于TeX的排版系统,广泛应用于学术论文、技术文档、图书出版等领域。其中,参考文献的展示方式是 Latex 的一个重要组成部分。本文将介绍如何修改...

    亚博智能科技BST-V51学习板全套资料

    视频,源代码,光盘配套一切资料

Global site tag (gtag.js) - Google Analytics