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 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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
看一下node类的定义,有的不是val+next结构的,而是val+left+right结构的。
[思维问题]:
以为要用bst走一遍以后再翻,没想到bst里面可以直接在处理中间节点时翻(指定值迭代、指定指针)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
dummy节点真的是完全不起作用的,我们返回的是dummy.next
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
涉及到linkedlist遍历的题目都要用两个指针:cur和prev
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
bst里面可以直接在处理中间节点时翻(指定值迭代、指定指针)
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class TreeNode {
int val;
TreeNode left, right; TreeNode(int item) {
val = item;
left = right = null;
}
} class Solution {
Node prev = null; public Node treeToDoublyList(Node root) {
//corner case
if (root == null) return null; //initialization: dummy node
Node dummy = new Node(0, null, null);
dummy.right = prev;
prev = dummy; //uitlize the function
inOrderTraversal(root); //connect the first and last
prev.right = dummy.right;
dummy.right.left = prev; //return
return dummy.right;
} public void inOrderTraversal(Node cur) {
//exit case
if (cur == null) return ; //traverse l, change cur & prev, r
inOrderTraversal(cur.left);
cur.left = prev;
prev.right = cur;
prev = cur;
inOrderTraversal(cur.right);
}
}
426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表的更多相关文章
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- [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 ...
- [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 ...
- Convert Binary Search Tree to Doubly Linked List
Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...
- Convert Binary Search Tree (BST) to Sorted Doubly-Linked List
(http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...
随机推荐
- devops的理解
DevOps(Development和Operations的组合词)是一种重视"软件开发人员(Dev)"和"IT运维技术人员(Ops)" DevOps是一组过程 ...
- PythonStudy——字典 Dictionary
# 容器(集合):存放多个值的变量# 单列容器(系统中的单列容器很多):list | tuple# 双列容器(map):只有dict,存放数据 成对出现,dict存放数据采用 key-value键值对 ...
- 理解java回调机制
摘自:ImportNew 以前不理解什么叫回调,天天听人家说加一个回调方法啥的,心里想我草,什么叫回调方法啊?然后自己就在网上找啊找啊找,找了很多也不是很明白,现在知道了,所谓回调:就是A类中调用B类 ...
- 自己动手,丰衣足食!Python3网络爬虫实战案例
本教程是崔大大的爬虫实战教程的笔记:网易云课堂 Python3+Pip环境配置 Windows下安装Python: http://www.cnblogs.com/0bug/p/8228378.html ...
- [UE4]Named Slot
用户创建的UI成为其他UI的子控件的时候,默认情况下是不能拥有子控件的,给UI添加一个Named Slot,这个UI就可以拥有子控件 一.创建一个名为testNameSlot的UI,添加3个Named ...
- 20165312 2017-2018-2《Java程序设计》课程总结
20165312 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...
- Error Code: 1786 Statement violates GTID consistency: CREATE TABLE ... SELECT.
1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:call account_check_main('20180511') 错误 ...
- Struts2 中常用的代码
BaseAction public class BaseAction extends ActionSupport { protected String target; public Map getRe ...
- Linux运维人员最常用166个命令汇总
引用自“菜鸟博客” 命令 功能说明 线上查询.帮助命令(2个) man 查看命令帮助,命令词典,更复杂还有info,但不常用. help 查看Linux内置命令的帮助,比如cd等命令. 文件.目录操作 ...
- Vue keep-alive的总结
1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗. <keep-alive> <component> <!- ...