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 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?
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.
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
相关推荐
4. **低功耗**:在标准模式下,每秒采样一次时电流消耗仅5μA。 5. **低噪声**: - 在超低功耗模式下,噪声为0.06 hPa (0.5m)。 - 在超高分辨率模式下,噪声可低至0.02 hPa (0.17m)。 6. **温度测量**:内置温度...
在1 Hz的采样率下,其电流消耗仅为2.7µA,这种低功耗的特性特别适合于电池供电的手持设备,如智能手机、智能手表等,使其可以在保持性能的同时,增加设备的续航能力。 工作温度范围从-40到+85°C,几乎覆盖了所有...
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 气体传感器的详细信息,这款传感器由 BOSCH 公司生产,主要用于环境温度和气压的精确测量。BMP180 是一款集成度高、性能稳定的传感器,广泛应用于气象站、智能家居、...
将bst文件放到MiKTeX的bst文件夹下自己新建的gbt7714-2005文件下,1)作者名称如 KERNER B S 的样式 GBT7714-2005NLang.bst 2)作者名称如 KERNER B S 的样式 GBT7714-2005NLang_UP.bst 3)作者名称如 Kerner B S 的...
4. **低功耗设计**:5μA/秒的功耗在保持一定精度的同时大大降低了能耗,尤其适用于移动设备。 5. **低噪声**: - 超低功耗模式下的噪声为0.06 hPa,相当于0.5米的高度差;超高分辨率模式下噪声为0.03 hPa,相当于...
在1Hz采样率下,电流消耗为2.7微安(µA)。 在温度性能方面,BMP280的温度范围覆盖-40至+85摄氏度,能够适应各种极端环境。此外,该传感器满足RoHS指令的要求,不含有害物质,符合环保标准。BMP280的设计使其具有1...
java java_leetcode题解之Convert BST to Greater Tree.java
《GBT7714-2005NLang.bst样式文件详解与应用》 在学术研究和出版领域,正确引用参考文献是至关重要的。中国国家标准GB/T 7714-2005《文后参考文献著录规则》为中文文献的引用提供了统一规范。其中,`GBT7714-2005...
根据提供的信息,我们可以了解到这是一份关于bst-4wd+V4.0发行版1的文档,主要关注的是与stm32微控制器相关的硬件设计细节。由于提供的文档内容较为复杂且部分信息不易解读,以下将重点围绕标题、描述以及部分可见...
在本压缩包中,我们聚焦于51单片机编程,特别是BST-V51型号的51单片机。这个集合包含了大量的实例代码,旨在帮助开发者深入理解和掌握51单片机的使用,以及与其相关的IIC接口和数码管显示技术。 首先,"51代码"是指...
**BST-BMP280-DS001-11.zip** 包含的是BOSCH Sensortec公司的BMP280传感器的数据手册。**BMP280** 是一款高度集成的数字压力和温度传感器,广泛应用于气象、环境监测、物联网设备以及消费电子产品中。这份数据手册是...
3. **兼容Android Lollipop系统**:支持显著运动检测及步数计数器功能,每项功能仅消耗5μA的电流。 4. **小型化设计**:尺寸仅为2.5×3.0毫米²,厚度为0.83毫米,非常适合空间有限的应用场合。 5. **内置电源管理...
根据提供的文档内容,我们可以了解到关于BME680传感器的详细知识点。BME680是由博世传感科技(Bosch Sensortec)开发的一款低功耗数字4合1环境传感器,具备测量气体、湿度、气压和温度的功能。以下是详细知识点: ...
在电流消耗方面,BMP280在1 Hz的采样率下,消耗电流仅为2.7微安(µA),这进一步突出了它的低功耗优势。该传感器符合RoHS标准,不含卤素,并且MSL等级为1,这些特点确保了BMP280具有很高的环境友好性和在制造过程中...
BMI088是一款六轴运动追踪的惯性测量单元(IMU),能够检测六个自由度(6DoF)内的移动和旋转。它集成了两个惯性传感器的功能于一个设备中:一个先进的三轴16位陀螺仪和一个多功能的、领先的三轴16位加速度计。...
视频,源代码,光盘配套一切资料
### BST-V51智能小车底板电路原理图 #### 一、整体概述 BST-V51智能小车是一款集成了多种传感器与执行器的智能设备,主要用于教育及科研领域。其底板电路原理图展示了该智能小车的核心硬件设计,包括舵机供电模块、...