讲二次搜索树转化为排序的双向链表
生活随笔
收集整理的這篇文章主要介紹了
讲二次搜索树转化为排序的双向链表
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.gylhaut.bean;public class TreeNode<T> {public T data;public TreeNode left;public TreeNode right;public TreeNode(T data) {this.left = null;this.right = null;this.data = data;}
}
算法實(shí)現(xiàn):
package com.gylhaut.util;import com.gylhaut.bean.TreeNode;public class BinaryTreeHelper {/*** 指向頭節(jié)點(diǎn)* @param root* @return*/public static TreeNode convert(TreeNode root){root=convert2Link(root);while (root.left != null){root = root.left;}return root;}/*** 搜索二叉樹轉(zhuǎn)成雙向鏈表* @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;}}
轉(zhuǎn)載于:https://www.cnblogs.com/gylhaut/p/10270880.html
總結(jié)
以上是生活随笔為你收集整理的讲二次搜索树转化为排序的双向链表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# JScript.Eval使用
- 下一篇: 什么是Servlet容器?