Convert a binary search tree to doubly linked list with in-order traversal.

Example

Given a binary search tree:

    4
/ \
2 5
/ \
1 3

return 1<->2<->3<->4<->5

中序遍历 穿针引线

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
* Definition for Doubly-ListNode.
* public class DoublyListNode {
* int val;
* DoublyListNode next, prev;
* DoublyListNode(int val) {
* this.val = val;
* this.next = this.prev = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of tree
* @return: the head of doubly list node
*/
public List<DoublyListNode> helper(List<DoublyListNode> hT, TreeNode root) {
if (root.left != null) {
helper(hT, root.left);
}
DoublyListNode newNode = new DoublyListNode(root.val);
newNode.prev = hT.get(1);
hT.get(1).next = newNode;
hT.remove(1);
hT.add(newNode);
if (root.right != null) {
helper(hT, root.right);
}
return hT;
}
public DoublyListNode bstToDoublyList(TreeNode root) {
// Write your code here
if (root == null) {
return null;
}
DoublyListNode head = new DoublyListNode(-1);
DoublyListNode tail = head;
List<DoublyListNode> hT = new ArrayList<DoublyListNode>();
hT.add(head);
hT.add(tail);
helper(hT, root);
hT.get(0).next.prev = null;
return hT.get(0).next;
}
}

Convert Binary Search Tree to Doubly Linked List的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 算法与数据结构基础 - 二叉查找树(Binary Search Tree)

    二叉查找树基础 二叉查找树(BST)满足这样的性质,或是一颗空树:或左子树节点值小于根节点值.右子树节点值大于根节点值,左右子树也分别满足这个性质. 利用这个性质,可以迭代(iterative)或递归 ...

随机推荐

  1. UVA 12345 Dynamic len(带修莫队)

    Dynamic len [题目链接]Dynamic len [题目类型]带修莫队 &题解: 莫队可以单点更改,只要再多加一维,代表查询次数,排序的时候3个关键字. 之后循环离线的时候,先暴力时 ...

  2. C#构造函数、私有构造函数、静态构造函数与构造函数执行顺序

    默认构造函数,如果没有为类指定任何构造函数,编译器会自动为类创建一个无参构造函数,用以初始化类的字段:如果为类编写了构造函数,那么编译器就不会再自动生成无参构造函数了.ps.C#不允许用户为结构定义无 ...

  3. 0001-20180421-自动化第一章-python基础学习笔记

    ======================学习python==================介绍: python种类: cpython(*),jpython,ironpython,rubypyth ...

  4. 创建servlet程序知识点详解---servlet-day12

    自定义标签 (1)编程步骤 step1 jsp标签分为复杂标签技术(old),简单标签(new) 注(了解) jsp标签技术分为复杂标签技术(old),简单标签技术(new) step2 ###MVC ...

  5. 使用curl自动签到百度贴吧

    百度贴吧的一键签到接口地址为 http://tieba.baidu.com/tbmall/onekeySignin1 curl http://tieba.baidu.com/tbmall/onekey ...

  6. rds下载备份集在另外一台机器上恢复并应用binlog日志

    -----------------------------备份还原并启动数据库----------------------------------1.创建目录,并把下载的压缩文件拷贝到相应的目录[ro ...

  7. Springboot 使用PageHelper分页插件实现分页

    一.pom文件中引入依赖 二.application.properties中配置以下内容(二选一方案) 第一种:pagehelper.helper-dialect=mysqlpagehelper.re ...

  8. 解决apache httpd列出目录列表中文乱码问题

    问题: 找了好几个方法都不对, 很多都是说修改AddDefaultCharset字段的, 下面是新的方法, 新测可行 在httpd.conf下, 随便找个地方把下面这个字段扔上去, 重启即可 Inde ...

  9. [c/c++] programming之路(19)、数组指针

    一.指针运算 #include<stdio.h> #include<stdlib.h> void main0(){ ; int *p=&a; printf());//变 ...

  10. SSM-网站后台管理系统制作(2)---SSM基本工作原理

    SSM基本工作原理 讲解网站:https://www.w3cschool.cn/wkspring/dcu91icn.html 构建基本工作环境: mysql eclipse(tomcat8.0) Hb ...