[抄题]:

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);
}
}

最新文章

  1. 解决:sudo: 无法解析主机:dinphy-500-310cn: 连接超时
  2. [转] HashMap的存取之美
  3. 剑指Offer 顺时针打印矩阵
  4. BestCoder#15 A-LOVE(暴力)
  5. Unity 之 人物换装
  6. Django数据模型及操作
  7. BZOJ3931 [CQOI2015]网络吞吐量(最大流)
  8. python中的 zip函数详解
  9. oracle-linux下挂载"移动硬盘" NTFS类型
  10. js基础例子dom+原型+oop基础知识记录01
  11. python10min系列之面试题解析:python实现tail -f功能
  12. 微信二维码扫描下载APK
  13. HDU——B-number(数字DP)
  14. sessionStorage用于分页,瀑布流和存储用户数据等
  15. 修改NSMutableArray中的元素时的注意事项
  16. c++ --> 返回值分析
  17. 架构之Nginx(负载均衡/反向代理)
  18. SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端、Eureka 服务信息、Eureka 发现管理、Eureka 安全配置、Eureka-HA(高可用) 机制、Eureka 服务打包部署)
  19. leetcode — generate-parentheses
  20. urlrewritefilter 本地windowsxp 上正常 使用 ,但是 到linux服务器 上 则时好时坏 ,不起作用

热门文章

  1. mig_7series_v4_0_data_gen_chk
  2. 深度图从ros数据类型转换成opencv数据类型
  3. 关于APS在企业生产计划上的应用
  4. 【mysql】批量更新数据
  5. Win7系统安装Centos7.0双系统(三)
  6. *浅解嵌入式中的BootLoader
  7. Git和代码规范
  8. PHP常用的转义函数
  9. 一个简单的通讯服务框架(大家发表意见一起研究)JAVA版本
  10. 【机器学习_7】numpy