博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
讲二次搜索树转化为排序的双向链表
阅读量:4309 次
发布时间:2019-06-06

本文共 1344 字,大约阅读时间需要 4 分钟。

package com.gylhaut.bean;public class TreeNode
{ public T data; public TreeNode left; public TreeNode right; public TreeNode(T data) { this.left = null; this.right = null; this.data = data; }}

 算法实现:

package com.gylhaut.util;import com.gylhaut.bean.TreeNode;public class BinaryTreeHelper {    /**     * 指向头节点     * @param root     * @return     */    public static TreeNode convert(TreeNode root){        root=convert2Link(root);        while (root.left != null){            root = root.left;        }        return root;    }    /**     * 搜索二叉树转成双向链表     * @param root     * @return     */    public static TreeNode convert2Link(TreeNode root){        if(root == null|| (root.left == null && root.right == null)){            return root;        }        TreeNode tmp = null;        if(root.left != null){           tmp= convert2Link(root.left);           while (tmp.right != null){               tmp = tmp.right;           }           tmp.right = root;           root.left = tmp;        }        if (root.right !=null){            tmp = convert2Link(root.right);            while (tmp.left != null){                tmp = tmp.left;            }            tmp.left = root;            root.right = tmp;        }        return root;    }}

  

转载于:https://www.cnblogs.com/gylhaut/p/10270880.html

你可能感兴趣的文章
linux 常用性能优化
查看>>
安装wazuh-agent
查看>>
修改windows网络参数,让上网更快
查看>>
利用nc当作备用shell管理方案.
查看>>
备用shell管理方案之butterfly+nginx+https
查看>>
使用开源软件 jumpserver 搭造自己的堡垒机
查看>>
[报错解决] "MySQL server has gone away" 解决思路
查看>>
http状态码-备查
查看>>
iptables一些练习
查看>>
常用命令备忘 xargs
查看>>
关于nginx反代jenkins报错 反向代理设置有误
查看>>
关于Ubuntu中snap安装软件太慢解决办法
查看>>
esp8266 + dht11 + 两路继电器 实现pc远程控制开关机温度监控.并配置zabbix监控
查看>>
在linux中设置优先使用ipv4,而不是ipv6
查看>>
谷歌浏览器离线安装包下载
查看>>
正则表达式
查看>>
AWK命令使用
查看>>
Redis项目实战---应用及理论(三)---Jedis使用
查看>>
Redis项目实战--应用及理论(一)--redis基础
查看>>
Redis项目实战---应用及理论(二)---Redis集群原理
查看>>