import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import javax.swing.tree.TreeNode;
public class Solution {
static treeNode last=null;
public static void main(String args[])
{
treeNode root=new treeNode();
root.value=5;
root.right=new treeNode();
root.right.value=10;
treeNode rootleft=new treeNode();
rootleft.value=8;
root.left=rootleft;
treeNode left=new treeNode();
left.value=4;
treeNode right=new treeNode();
right.value=9;
rootleft.left=left;
rootleft.right=right;
fun(root);
treeNode record=last;
while(last!=null)
{
System.out.println(last.value);
last=last.left;
}
last=record;
while(last.left!=null)
{
last.left.right=last;
last=last.left;
}
while(last!=null)
{
System.out.println(last.value);
last=last.right;
}
}
static void fun(treeNode root)
{
if(root.left!=null)
{
fun(root.left);
}
root.left=last;
last=root;
if(root.right!=null)
{
fun(root.right);
}
}
}
class treeNode
{
int value;
treeNode left=null;
treeNode right=null;
}
常量空间把一个二叉树变成链表
最新推荐文章于 2025-03-24 22:28:40 发布
本文介绍了一种将二叉搜索树转换为双向链表的方法,并提供了完整的Java实现代码。通过对二叉树进行中序遍历,可以有效地将树结构转化为有序的双向链表,便于进一步的数据处理。

483

被折叠的 条评论
为什么被折叠?



