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. 020-Json结构数据序列化异步传递

    C#中将.Net对象序列化为Json字符串的方法: JavaScriptSerializer().Serialize(p),JavaScriptSerializer在System.Web.Extens ...

  2. SpringMVC 图片上传,检查图片大小

    使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...

  3. LinkedHashMap和HashTable

    LinkedHashMap: 继承了HashMap: 其中,key不允许重复是Map接口就有的性质: HashTable: 同步的,意味着是单线程,意味着线程安全的,但是速度慢,和List接口集合的子 ...

  4. MariaDB glare cluster简介

    MariaDB MariaDB 是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器,MariaDB是同一MySQL版本的二进制替代品, 当前最新版本1 ...

  5. 51Nod 1058 N的阶乘的长度

    输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.   Input 输入N(1 <= N <= 10^6) Output 输出N的阶乘的长度 Input示例 6 Out ...

  6. SpringMVC登录拦截DEMO

    交给/login的post请求的控制器处理, 并通过控制器的逻辑控制获取提示信息login.jsp<%-- Created by IntelliJ IDEA. User: shijinglu D ...

  7. 如何将数据库中的数据导入到Solr中

    要使用solr实现网站中商品搜索,需要将mysql数据库中数据在solr中创建索引. 1.需要在solr的schema.xml文件定义要存储的商品Field. 商品表中的字段为: 配置内容是: < ...

  8. MySQL5.7 Dockerfile

    #Dockerfile for mysql5.7 FROM centos COPY ["src","/src"] RUN groupadd -g 1003 my ...

  9. PXC添加新节点

    先拉数据,再启用节点,可以避免SST 拉数据 [root@pxc_node1_172.16.11.132 ~]# /usr/bin/innobackupex --defaults-file=/etc/ ...

  10. nginx-负载均衡相关配置 第五章

    一.负载均衡: 通过反向代理客户端的请求到一个服务器群组,通过某种算法,将客户端的请求按照自定义的有规律的一种调度调度给后端服务器. Nginx的负载均衡使用upstream定义服务器组,后面跟着组名 ...