import java.util.Arrays;
import java.util.Scanner;
public class Main {
private static class TreeNode {
TreeNode left;
TreeNode right;
int data;
TreeNode(int newData) {
left = null;
right = null;
data = newData;
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int[] pre =new int[n];
for(int i=0;i<n;i++)
pre[i] =input.nextInt();
int[] in =new int[n];
for(int i=0;i<n;i++)
in[i] =input.nextInt();
input.close();
TreeNode treeNode = reConstructBinaryTree(pre, in);
prePost(treeNode);
}
public static TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length == 0 || in.length == 0)
return null;
TreeNode root = new TreeNode(pre[0]);
for (int i = 0; i < in.length; i++) {
if(in[i] == pre[0]){
//左子树,copyOfRange函数是左闭右开
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i));
//递归构建左子树
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
break;
}
}
return root;
}
public static void prePost(TreeNode root){
if(root == null)
System.out.println("空树");
if(root.left != null)
prePost(root.left);
if(root.right!= null)
prePost(root.right);
System.out.print(root.data+" ");
}
}
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
public class Main2 {
private static class TreeNode {
TreeNode left;
TreeNode right;
int data;
TreeNode(int newData) {
left = null;
right = null;
data = newData;
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] pre = new int[n];
for (int i = 0; i < n; i++)
pre[i] = input.nextInt();
int[] in = new int[n];
for (int i = 0; i < n; i++)
in[i] = input.nextInt();
input.close();
TreeNode treeNode = reConstructBinaryTree(pre, in);
prePost(treeNode);
}
public static TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre == null || pre.length == 0) {
return null;
}
TreeNode root = new TreeNode(pre[0]);
Deque<TreeNode> stack = new LinkedList<TreeNode>();
stack.push(root);
int inorderIndex = 0;
for (int i = 1; i < pre.length; i++) {
int preorderVal = pre[i];
TreeNode node = stack.peek();
if (node.data != in[inorderIndex]) {
node.left = new TreeNode(preorderVal);
stack.push(node.left);
} else {
while (!stack.isEmpty() && stack.peek().data == in[inorderIndex]) {
node = stack.pop();
inorderIndex++;
}
node.right = new TreeNode(preorderVal);
stack.push(node.right);
}
}
return root;
}
public static void prePost(TreeNode root) {
if (root == null)
System.out.println("空树");
if (root.left != null)
prePost(root.left);
if (root.right != null)
prePost(root.right);
System.out.print(root.data + " ");
}
}
这个树节点一般用递归实现,如果是二叉树的话可以改用数组,但是浪费空间很大,你首先要熟悉二叉树的5个性质。
更详细的了解可以看看这个:https://edu.csdn.net/course/detail/4583
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632