java构造一个二叉树,并先序、中序、后序进行遍历
时间: 2023-04-26 12:01:21 浏览: 90
Java实现二叉树的先序、中序、后续、层次遍历
首先,我们需要定义一个二叉树的节点类,包含节点值、左子节点和右子节点:
```
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
```
然后,我们可以通过递归的方式构造一个二叉树。假设我们有一个数组 `preorder` 存储了二叉树的先序遍历结果,数组 `inorder` 存储了二叉树的中序遍历结果,那么我们可以按照以下步骤构造二叉树:
1. 从 `preorder` 中取出第一个元素作为根节点的值 `rootVal`。
2. 在 `inorder` 中找到 `rootVal` 的位置,将数组分为左子树和右子树两部分。
3. 递归构造左子树和右子树,分别将左子树和右子树的根节点作为当前节点的左子节点和右子节点。
具体实现如下:
```
public TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
private TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int rootVal = preorder[preStart];
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
int leftSize = rootIndex - inStart;
root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSize, inorder, inStart, rootIndex - 1);
root.right = buildTreeHelper(preorder, preStart + leftSize + 1, preEnd, inorder, rootIndex + 1, inEnd);
return root;
}
```
最后,我们可以分别实现二叉树的先序、中序、后序遍历。以先序遍历为例:
```
public void preorderTraversal(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preorderTraversal(root.left);
preorderTraversal(root.right);
}
```
完整代码如下:
```
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class BinaryTree {
public static void main(String[] args) {
int[] preorder = {1, 2, 4, 5, 3, 6, 7};
int[] inorder = {4, 2, 5, 1, 6, 3, 7};
TreeNode root = buildTree(preorder, inorder);
System.out.print("先序遍历:");
preorderTraversal(root);
System.out.println();
System.out.print("中序遍历:");
inorderTraversal(root);
System.out.println();
System.out.print("后序遍历:");
postorderTraversal(root);
System.out.println();
}
public static TreeNode buildTree(int[] preorder, int[] inorder) {
return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
private static TreeNode buildTreeHelper(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
int rootVal = preorder[preStart];
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
int leftSize = rootIndex - inStart;
root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSize, inorder, inStart, rootIndex - 1);
root.right = buildTreeHelper(preorder, preStart + leftSize + 1, preEnd, inorder, rootIndex + 1, inEnd);
return root;
}
public static void preorderTraversal(TreeNode root) {
if (root == null) {
return;
}
System.out.print(root.val + " ");
preorderTraversal(root.left);
preorderTraversal(root.right);
}
public static void inorderTraversal(TreeNode root) {
if (root == null) {
return;
}
inorderTraversal(root.left);
System.out.print(root.val + " ");
inorderTraversal(root.right);
}
public static void postorderTraversal(TreeNode root) {
if (root == null) {
return;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
System.out.print(root.val + " ");
}
}
```
阅读全文