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 ...
随机推荐
- Python 规范化LinkedIn用户联系人的职位名
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-19 @author: guaguastd @name: j ...
- 字符串各个字符ASCII值加5
程序实现目标: 输入一个字符串,将其各个字符对应的ASCII值加5后,输出结果 程序要求:该字符串只包含小写字母,若其值加5后的字符值大于'z',将其转换成从a开始的字符. 分析:问题归结为三点: 1 ...
- 从epoll构建muduo-12 多线程入场
mini-muduo版本号传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ve ...
- iis出现HTTP 错误 403.14 - Forbidden Web问题
找到"目录浏览",并"应用"
- TP 框架 如果去掉表前缀
#jd_admin_abc 去掉前缀 C('DB_PREFIX')=获取前缀 结果为admin_abc $table_Name=str_replace(C('DB_PREFIX'), '', $tab ...
- C打印函数printf的一种实现原理简要分析
[0]README 0.1)本文旨在对 printf 的 某一种 实现 原理进行分析,做了解之用: 0.2) vsprintf 和 printf.c 的源码,参见 https://github.com ...
- Android-解决Fail to post notification on channel "null"的方法
原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...
- 【峰回路转】Excel技巧百例 08.计算两个日期的差值
在Excel中假设高速计算两个日期之间的差? 比如A日期为:2012/3/12 B日期为:2015/7/29 那么这两个日期之间差几年,差几个月.差多少天? 我们使用DateDif 函数来处理. ...
- GS与网络打交道
与网络打交道 在GS,GC,Share都与网络打交道,但还是GC最多 GC打交道过程 send_stat BaseChannel::SendCmdTry() { if (!m_queCmd.size( ...
- TensorFlowSharp
https://github.com/migueldeicaza/TensorFlowSharp