题目:

剑指offer的题目有挺多都挺典型的,就像这一道。不过书中的代码写的真是ugly,有很多题目LeetCode上都有,可以去LeetCode讨论区看看,经常有一些大神分享,写的代码真是高效、简洁、清晰,剑指offer上的代码不仅变量名定义又长又不知所云,让人看着就很不清晰明了,还有各种没必要的判断让代码看起来非常不直观。

思路:书中已经写的挺清楚了,通过中序遍历来做。

package com.ss.offer;

/**
* 2018-09-15 下午11:18.
*
* @author blank
*/
public class BST2LinkedList {
public static void main(String[] args) throws Exception {
BinaryTreeNode root = new BinaryTreeNode(10);
BinaryTreeNode six = new BinaryTreeNode(6);
BinaryTreeNode four = new BinaryTreeNode(4);
BinaryTreeNode eight = new BinaryTreeNode(8);
BinaryTreeNode fourteen = new BinaryTreeNode(14);
BinaryTreeNode twelve = new BinaryTreeNode(12);
BinaryTreeNode sixteen = new BinaryTreeNode(16);
root.left = six;
root.right = fourteen;
six.left = four;
six.right = eight;
four.left = null;
four.right = null;
eight.left = null;
eight.right = null;
fourteen.left = twelve;
fourteen.right = sixteen;
twelve.left = null;
twelve.right = null;
sixteen.right = null;
sixteen.left = null;
BinaryTreeNode res = convert(root);
while (res != null) {
System.out.println(res.val);
res = res.right;
}
} static BinaryTreeNode convert(BinaryTreeNode root) { BinaryTreeNode[] lastNode = new BinaryTreeNode[1];
convertNode(root, lastNode);
BinaryTreeNode headNode = lastNode[0];
while (headNode.left != null)
headNode = headNode.left;
return headNode; } static void convertNode(BinaryTreeNode root, BinaryTreeNode[] last) {
if (root == null) {
return;
}
convertNode(root.left, last);
if (last[0] != null) {
last[0].right = root;
}
root.left = last[0];
last[0] = root;
convertNode(root.right, last);
} private static class BinaryTreeNode {
int val;
BinaryTreeNode left;
BinaryTreeNode right; public BinaryTreeNode(int val) {
this.val = val;
}
}
}

剑指offer——27. 二叉搜索树与双向链表(Java版)的更多相关文章

  1. 剑指offer 27二叉搜索树与双向链表

    class Solution { public: void ConvertNode(TreeNode* pRootOfTree,TreeNode** pre) { if(pRootOfTree) { ...

  2. 剑指 Offer 36. 二叉搜索树与双向链表 + 中序遍历 + 二叉排序树

    剑指 Offer 36. 二叉搜索树与双向链表 Offer_36 题目描述 题解分析 本题考查的是二叉树的中序遍历以及二叉排序树的特征(二叉排序树的中序遍历序列是升序序列) 利用排序二叉树中序遍历的性 ...

  3. 剑指 Offer 36. 二叉搜索树与双向链表

    剑指 Offer 36. 二叉搜索树与双向链表 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表.要求不能创建任何新的节点,只能调整树中节点指针的指向. 为了让您更好地理解问题,以下面的 ...

  4. 【剑指Offer】二叉搜索树与双向链表 解题报告(Python)

    [剑指Offer]二叉搜索树与双向链表 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interview ...

  5. 【Java】 剑指offer(36) 二叉搜索树与双向链表

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不 ...

  6. 【剑指offer】二叉搜索树转双向链表,C++实现

    原创博文,转载请注明出处! # 题目 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 二叉树节点的定义 struct TreeNod ...

  7. Go语言实现:【剑指offer】二叉搜索树与双向链表

    该题目来源于牛客网<剑指offer>专题. 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. Go语言实现: type T ...

  8. 【剑指offer】二叉搜索树转双向链表

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/26623795 题目描写叙述: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表. ...

  9. 《剑指offer》二叉搜索树和双向链表

    本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:

随机推荐

  1. Excel&&word&&PPT

    1. Excel 1.1 制作下拉框 选中单元格或列--> 菜单"数据" --> "数据验证"-->"设置" --> ...

  2. bnu 28890 &zoj 3689——Digging——————【要求物品次序的01背包】

    Digging Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...

  3. 吴恩达《Machine Learning Yearning》总结(31-40章)

    31.解读学习曲线:其他情况 下图反映了高方差,通过增加数据集可以改善. 下图反映了高偏差和高方差,需要找到一种方法来同时减少方差和偏差. 32.绘制学习曲线 情况:当数据集非常小时,比如只有100个 ...

  4. 2017年11月4日 vs类和结构的区别&哈希表&队列集合&栈集合&函数

    类和结构的区别 类: 类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存 类有构造和析构函数 类可以继承和被继承 结构: 结构是值类型在栈上分配(虽然栈的访问速度比 ...

  5. swagger快速开发

    转载:https://blog.csdn.net/xxoo00xx00/article/details/77163399 swagger 学习笔记 搭建环境: 1,jdk1.8 2,idea 3,sp ...

  6. Format - DateTime

    1. Long Date/Short Date/Long Time/Short Time,可以在系统的“Region and Language”中找到相应设置: 2. ISO Format/Local ...

  7. matlab练习程序(结构张量structure tensor)

    根据结构张量能区分图像的平坦区域.边缘区域与角点区域. 此算法也算是计算机科学最重要的32个算法之一了.链接的文章中此算法名称为Strukturtensor算法,不过我搜索了一下,Strukturte ...

  8. Siebel 开发规范

    Siebel Configuration and Development Guideline 1 2 2.1 2.2 2.3 11. 2.4 2.5 3 3.1 3.2 3.2.1 3.2.2 3.3 ...

  9. GridView——标题行自适应单元格列宽与选中单元格变色

    首先看效果图: 主要实现—— 1.前台GridView代码: <asp:GridView Height="100%" Width="98%" ID=&qu ...

  10. SQL Server ->> CLR存储过程枚举目录文件并返回结果集

    因工作需要写了个CLR存储过程枚举目录文件并返回结果集 using System; using System.IO; using System.Collections.Generic; using S ...