LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
原题链接在这里:https://leetcode.com/problems/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.
题解:
The order is inorder traversal.
Use stack to help inorder traversal. When root is not null, push it into stack and move to its left child.
When root is null, stack is not empty, pop the top and append it to current node's right. Then root = top.right.
When root is null, stack is empty, assign current node's right to dummy.right, and dummy.right.left = current.
Time Complexity: O(n).
Space: O(h).
AC Java:
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right; public Node() {} public Node(int _val,Node _left,Node _right) {
val = _val;
left = _left;
right = _right;
}
};
*/
class Solution {
public Node treeToDoublyList(Node root) {
if(root == null){
return root;
} Stack<Node> stk = new Stack<Node>();
Node dummy = new Node();
Node cur = dummy;
while(root!=null || !stk.isEmpty()){
if(root != null){
stk.push(root);
root = root.left;
}else{
Node top = stk.pop();
cur.right = top;
top.left = cur;
cur = cur.right;
root = top.right;
}
} cur.right = dummy.right;
dummy.right.left = cur; return dummy.right;
}
}
类似Binary Tree Inorder Traversal.
LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List的更多相关文章
- [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 ...
- 【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 ...
- [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 ...
- [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 ...
- 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 point ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode#272] Closest Binary Search Tree Value II
Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are ...
- [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...
随机推荐
- ARM和STM32的区别及ARM公司架构的发展
ARM和STM32的区别及ARM公司架构的发展 转:https://www.cnblogs.com/kwdeblog/p/5260348.html ARM是英国的芯片设计公司,其最成功的莫过于32位嵌 ...
- Splash动画启动app时空白屏
相信大多数人一开始都会对启动app的时候出现先白瓶或者黑屏然后才进入第一个界面,例如:SplashActivity.那这是什么原因造成的呢? <style name="Splash_T ...
- refresh的停车场(栈和队列的STL)
refresh的停车场 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 refresh近期发了一笔横財,开了一家停车场. 因 ...
- anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg
转载自:http://blog.csdn.net/qingyanyichen/article/details/73550924 本人下载编译安装了opencv2.4.9,oppencv2.4.10,o ...
- 精彩回顾 HUAWEI HiAI 亮相华为北研所
从普通照片变成艺术品,仅需3秒: 从随手拍下的讲解胶片到生成规整清晰的ppt,只要瞬间…… 5月25日在华为北京研究所举办的HUAWEI HiAI技术合作交流会上,伴随着一声声惊叹,数款接入HUA ...
- 初识vue-01
一.属性和方法 vue自定义的一些数据和方法需要绑定到实例的不同属性上面去例如数据都要绑定要data属性,方法都要绑定到methods方法实例上的data和methods里面的key值会自动挂载到vu ...
- Cocos2d-js官方完整项目教程翻译:六、添加Chipmunk物理引擎在我们的游戏世界里
添加Chipmunk物理引擎在我们的游戏世界里 一.简介 cocos2d JS能给我们力量来创造令人印象深刻的游戏世界.但缺乏某种现实. ...
- git服务的安装和使用
参考文章 http://www.centoscn.com/image-text/install/2014/0514/2972.html 1.搭建Git服务器yum安装Git服务器创建一个git用户,用 ...
- 【题解】[APIO2009]会议中心
[题解][P3626 APIO2009]会议中心 真的是一道好题!!!刷新了我对倍增浅显的认识. 此题若没有第二份输出一个字典序的方案,就是一道\(sort+\)贪心,但是第二问使得我们要用另外的办法 ...
- 我的Android进阶之旅------>Android实现音乐示波器、均衡器、重低音和音场功能
本实例来自于<疯狂Android讲义>,要实现具体的功能,需要了解以下API: MediaPlayer 媒体播放器 Visualizer 频谱 Equalizer 均衡器 BassBoo ...