bstToDoublyList

*/-->

code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}

bstToDoublyList

Table of Contents

1 描述

将一个二叉查找树按照中序遍历转换成双向链表。

2 样例

给定一个二叉查找树:

    4
/ \
2 5
/ \
1 3

返回 1<->2<->3<->4<->5

3 解决方案

做一个中序递归,用head保存首节点,用tail不断更新之后的每个节点。

/**
* 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 {
private DoublyListNode head = null;
private DoublyListNode tail = null;
/**
* @param root: The root of tree
* @return: the head of doubly list node
*/
public DoublyListNode bstToDoublyList(TreeNode root) {
// Write your code here
if (null == root) {
return null;
}
bstToDoublyList(root.left); DoublyListNode node = new DoublyListNode(root.val);
if (null == head) {
head = node;
tail = node;
} else {
tail.next = node;
node.prev = tail;
tail = node;
} bstToDoublyList(root.right); return head;
}
}

Date: 2017-06-26 21:57

Author: WEN YANG

Created: 2017-06-26 Mon 22:03

Emacs 25.2.1 (Org mode 8.2.10)

Validate

bstToDoublyList的更多相关文章

  1. lintcode:将二叉查找树转换成双链表

    题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...

  2. 剑指offer ------ 刷题总结

    面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...

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

  4. lintcode 刷题记录··

    单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...

  5. LintCode-378.将二叉查找树转换成双链表

    将二叉查找树转换成双链表 将一个二叉查找树按照中序遍历转换成双向链表. 样例 给定一个二叉查找树: 返回 1<->2<->3<->4<->5. 标签 链 ...

随机推荐

  1. 攻防世界--Hello, CTF

    测试文件地址:https://www.lanzous.com/i5ot1yd 使用IDA1打开 打开之后,这个字符串和第一题的有些类似,拿去转换一下,Flag就得到了 CrackMeJustForFu ...

  2. vue 页面切换从右侧切入效果

    1.将切换的页面用transition包裹 <div class="index-content"> <transition> <router-view ...

  3. Kvm--03 kvm克隆,桥接网络,热添加

    目录 1.Kvm克隆 1). 完整克隆 2). 链接克隆 2.kvm虚拟机的桥接网络 3.在线热添加网卡,cpu 1). 热添加网卡 2). 热添加内存 3). 热添加cpu参数 1.Kvm克隆 1) ...

  4. 树的计数 Prüfer编码与Cayley公式 学习笔记

    最近学习了Prüfer编码与Cayley公式,这两个强力的工具一般用于解决树的计数问题.现在博主只能学到浅层的内容,只会用不会证明. 推荐博客:https://blog.csdn.net/moreja ...

  5. Swoole 简单学习(2)

    Swoole 简单学习(2) swoole之tcp服务器: //创建tcp服务器new swoole_server(string $host,int $port,int $mode=SWOOLE_PR ...

  6. 日期格式化:SimpleDateFormat【线程不安全】、FastDateFormat和Joda-Time【后两个都是线程安全】

    SimpleDateFormat是线程不安全的,不能多个线程公用.而FastDateFormat和Joda-Time都是线程安全的,可以放心使用. SimpleDateFormat是JDK提供的,不需 ...

  7. Sass函数:Sass Maps的函数-map-has-key($map,$key)

    map-has-key($map,$key) 函数将返回一个布尔值.当 $map 中有这个 $key,则函数返回 true,否则返回 false. 前面的示例,当 $key 不在 $map 中时,使用 ...

  8. loj6177 「美团 CodeM 初赛 Round B」送外卖2 最短路+状压dp

    题目传送门 https://loj.ac/problem/6177 题解 一直不知道允不允许这样的情况:取了第一的任务的货物后前往配送的时候,顺路取了第二个货物. 然后发现如果不可以这样的话,那么原题 ...

  9. CentOS7.5 安装部署Apache+Mysql+Php

    系统:CentOS7.5 安装Apache 安装 yum -y install httpd 开启apache服务 systemctl start httpd.service 设置apache服务开机启 ...

  10. Radical and array

    Radical and array 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 27[提交][状态] 题目描述 Radical has an array , he wan ...