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+" ");
}
}
这个怎么改为非递归形式实现?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- allway2 2021-04-03 20:10关注
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 + " "); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 做个有关计算的小程序
- ¥15 MPI读取tif文件无法正常给各进程分配路径
- ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
- ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
- ¥15 setInterval 页面闪烁,怎么解决
- ¥15 如何让企业微信机器人实现消息汇总整合
- ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
- ¥15 如何用Python爬取各高校教师公开的教育和工作经历
- ¥15 TLE9879QXA40 电机驱动
- ¥20 对于工程问题的非线性数学模型进行线性化