import java.util.Arrays; public class Create { public TreeNode CreateTree(int[] preorder,int[] inorder) { if (preorder.length == 0 || inorder.length == 0) { return null; //前序 中序 长度都为0 返回空 } TreeNode root = new TreeNode(preorder[0]);//从前序中获取根节点。 int rootIndex = 0; //获取根节点在中序遍历中的位置 for (int i = 0; i < inorder.length; i++) { if (root.val == inorder[i]) { rootIndex = i; } } int[] Inorderleft = Arrays.copyOfRange(inorder, 0, rootIndex);//把中序遍历从根节点拆开左右两部分 这是左部分 int[] Inorderright = Arrays.copyOfRange(inorder, rootIndex + 1, inorder.length);//把中序遍历从根节点拆开 右部分 int[] Preorderleft= Arrays.copyOfRange(preorder,0,rootIndex);//把先序遍历已根节点 int[] Preorderright=Arrays.copyOfRange(preorder,rootIndex+1,preorder.length); root.left=CreateTree(Preorderleft,Inorderleft); root.right=CreateTree(Preorderright,Inorderright); return root; } }
时间: 2024-04-17 19:29:39 浏览: 14
这段代码定义了一个名为`Create`的类,并且包含了一个名为`CreateTree`的方法。该方法用于根据给定的前序遍历数组`preorder`和中序遍历数组`inorder`创建一棵二叉树。
方法首先进行了一些判断,如果前序遍历数组或中序遍历数组的长度为0,则说明没有节点可供创建,直接返回空。
接下来,方法创建了一个根节点`root`,其值为前序遍历数组的第一个元素`preorder[0]`。
然后,通过遍历中序遍历数组,找到根节点在其中的位置`rootIndex`。
接着,使用`Arrays.copyOfRange()`方法将中序遍历数组分成两部分,左部分为`Inorderleft`(从0到`rootIndex-1`),右部分为`Inorderright`(从`rootIndex+1`到数组末尾)。
同样地,使用`Arrays.copyOfRange()`方法将前序遍历数组也分成两部分,左部分为`Preorderleft`(从0到`rootIndex-1`),右部分为`Preorderright`(从`rootIndex+1`到数组末尾)。
接下来,通过递归调用`CreateTree()`方法,分别以左子树的前序遍历数组和中序遍历数组创建左子树,并以右子树的前序遍历数组和中序遍历数组创建右子树。
最后,将左子树和右子树分别赋值给根节点的`left`和`right`属性,并返回根节点`root`。
这样就完成了根据前序遍历和中序遍历数组创建二叉树的操作。
相关问题
import java.util.ArrayList; import java.util.Arrays; public class Create { public TreeNode CreateTree(int[] preorder,int[] inorder) { if (preorder.length == 0 || inorder.length == 0) { return null; //前序 中序 长度都为0 返回空 } TreeNode root = new TreeNode(preorder[0]);//从前序中获取根节点。 int rootIndex = 0; //获取根节点在中序遍历中的位置 for (int i = 0; i < inorder.length; i++) { if (root.val == inorder[i]) { rootIndex = i; } } int[] Inorderleft = Arrays.copyOfRange(inorder, 0, rootIndex);//把中序遍历从根节点拆开左右两部分 这是左部分 int[] Inorderright = Arrays.copyOfRange(inorder, rootIndex + 1, inorder.length);//把中序遍历从根节点拆开 右部分 int[] Preorderleft= Arrays.copyOfRange(preorder,1,rootIndex);//先序遍历从第一个非根节点的起到根节点在中序中所对应的位置号分为左部分 int[] Preorderright=Arrays.copyOfRange(preorder,rootIndex,preorder.length);//右部分 root.left=CreateTree(Preorderleft,Inorderleft); root.right=CreateTree(Preorderright,Inorderright); return root; } }
您好!您的代码是用于根据前序遍历和中序遍历构建二叉树的函数。这个函数通过递归地构建左子树和右子树来创建整个二叉树。首先,它从前序遍历中获取根节点,然后在中序遍历中找到根节点的索引位置。然后,它将中序遍历拆分为左部分和右部分,并将前序遍历也相应地拆分为左部分和右部分。接下来,它递归地调用自身来构建左子树和右子树,并将其连接到根节点上。最后,它返回根节点。这样就完成了二叉树的构建。
如果您有任何关于这段代码的问题,请随时提问!
package step1; /** * @author pengzheng */ import java.util.Arrays; // 导入数组工具类 Arrays import java.util.Scanner; public class TestIntArray { public static void main(String[] args) { … for (int e : a) { // 使用增强型for循环遍历数组 System.out.print(e + " "); } } }
好的,以下是一个基本数据类型数组的练习题:
请编写一个程序,要求用户输入一个整数n,然后创建一个长度为n的整型数组,再依次从键盘读入n个整数,并将它们存储到数组中。最后,输出数组中的所有元素(可以使用增强型for循环)。
示例输入:
5
1
2
3
4
5
示例输出:
输入的数组为:[1, 2, 3, 4, 5]
参考代码:
```java
import java.util.Arrays;
import java.util.Scanner;
public class TestIntArray {
public static void main(String[] args) {
// 创建Scanner对象
Scanner scanner = new Scanner(System.in);
// 读取数组长度
System.out.print("请输入数组长度:");
int length = scanner.nextInt();
// 创建数组
int[] arr = new int[length];
// 循环读取数组元素
for (int i = 0; i < length; i++) {
System.out.print("请输入数组第" + (i + 1) + "个元素:");
arr[i] = scanner.nextInt();
}
// 输出数组
System.out.println("输入的数组为:" + Arrays.toString(arr));
// 使用增强型for循环遍历数组并输出
for (int e : arr) {
System.out.print(e + " ");
}
}
}
```