bstToDoublyList
bstToDoublyList
*/-->
code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}
bstToDoublyList
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
bstToDoublyList的更多相关文章
- lintcode:将二叉查找树转换成双链表
题目 将一个二叉查找树按照中序遍历转换成双向链表 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5. ...
- 剑指offer ------ 刷题总结
面试题3 -- 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 1. 每行中的整数从左到右是排序的. 2. 每行的第一个数大于上一行的最后一个整数. publi ...
- 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 ...
- lintcode 刷题记录··
单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...
- LintCode-378.将二叉查找树转换成双链表
将二叉查找树转换成双链表 将一个二叉查找树按照中序遍历转换成双向链表. 样例 给定一个二叉查找树: 返回 1<->2<->3<->4<->5. 标签 链 ...
随机推荐
- vector中数据释放崩溃问题
struct LINE { char securityID[32]; /*!< 证券代码 */ int64_t dateTime; /*!< 日期时间(日期变化)YYYYMMDDhhmms ...
- 如何用latex画一个简单的表格
latex毫无疑问是一个十分强大的论文写作工具,所以掌握它就显得非常有意义,讲一下如何画一个简单的表格,代码如下: \begin{table}\centering\begin{tabular}{||c ...
- ios-实现ARC与MRC混编
选择target -> build phases -> compile sources -> 用ARC的文件将compiler flags设置为:-fobjc-arc,用MRC的文件 ...
- rpm - RPM 软件包管理器
SYNOPSIS 查询和校验软件包: rpm {-q|--query} [select-options] [query-options] rpm {-V|--verify} [select-optio ...
- 二、小程序内嵌Html基础格式说明
1.index.js文件更改 var WxParse = require('../../wxParse/wxParse.js'); Page({ data: { }, onLoad: function ...
- 从零开始之uboot、移植uboot2017.01(一、移植前的准备)
手边的是一个S5PV210的开发板,想尝试移植一个比较新的uboot 下载最新版本uboot2018. ftp://ftp.denx.de/pub/u-boot/ 编译器下载 http://www.v ...
- 使用字节流(InputStream、OutputStream)简单完成对文件的复制
文件的复制 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; imp ...
- Java中的String、StringBuffer、StringBuilder区别以及Java之StringUtils的用法
1.String.StringBuffer.StringBuilder的区别 String是Java中基础类型,是immutable类(不可变)的典型实现,利用string进行拼接是会产生过多无用对象 ...
- python之字符串切分
在工作中,经常遇到字符串切分,尤其是操作linux命令,返回一段文本,如下面这种格式 Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G ...
- ini操作
关于C#操作INI文件的总结 INI文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下: [Section1] key = value2 key = value2 …… [Sectio ...