java 二叉树转数组_Java实现二叉树的(根据一维数组)构建和先序中序后序遍历

Java实现二叉树

1. 定义结点类

public class TreeNode {

int val = 0;

TreeNode left = null;

TreeNode right = null;

public TreeNode(int val) {

this.val = val;

}

}

2. 构建二叉树

5bc5b1c91a0c964a37caa621eda61b33.png

public static TreeNode createBT(int[] arr, int i) // 初始时,传入的i==0

{

TreeNode root = null; // 定义根节点

if (i >= arr.length) // i >= arr.length 时,表示已经到达了根节点

return null;

root = new TreeNode(arr[i]); // 根节点

root.left = createBT(arr, 2*i+1); // 递归建立左孩子结点

root.right = createBT(arr, 2*i+2); // 递归建立右孩子结点

return root;

}

3. 遍历二叉树

3.1 先序遍历

// 先序遍历

public static void PreOrder(TreeNode root)

{

if (root == null)

return;

System.out.print(root.val+" ");

PreOrder(root.left);

PreOrder(root.right);

}

3.2 中序遍历

// 中序遍历

public static void InOrder(TreeNode root)

{

if (root == null)

return;

InOrder(root.left);

System.out.print(root.val+" ");

InOrder(root.right);

}

3.3 后序遍历

// 后序遍历

public static void PostOrder(TreeNode root)

{

if (root == null)

return;

PostOrder(root.left);

PostOrder(root.right);

System.out.print(root.val+" ");

}

4. 总代码

package com.BinaryTree;

// 定义(链式存储)二叉树结点

class TreeNode {

int val;

TreeNode left;

TreeNode right;

TreeNode(int x) { val = x; }

}

public class Main {

public static void main(String[] args) {

int[] arr = {1, 2, 3, 4, 5, 6};

TreeNode root = createBT(arr, 0);

System.out.println("先序遍历:");

PreOrder(root);

System.out.println("\n中序遍历:");

InOrder(root);

System.out.println("\n后序遍历:");

PostOrder(root);

}

// 使用一维数组建树(利用性质:在一维数组中, 第i个结点的左孩子结点是第2*i+1个结点, 第i个结点的右孩子结点是第2*i+2个结点)

public static TreeNode createBT(int[] arr, int i)

{

TreeNode root = null; // 定义根节点

if (i >= arr.length) // i >= arr.length 时,表示已经到达了根节点

return null;

root = new TreeNode(arr[i]); // 根节点

root.left = createBT(arr, 2*i+1); // 递归建立左孩子结点

root.right = createBT(arr, 2*i+2); // 递归建立右孩子结点

return root;

}

// 先序遍历

public static void PreOrder(TreeNode root)

{

if (root == null)

return;

System.out.print(root.val+" ");

PreOrder(root.left);

PreOrder(root.right);

}

// 中序遍历

public static void InOrder(TreeNode root)

{

if (root == null)

return;

InOrder(root.left);

System.out.print(root.val+" ");

InOrder(root.right);

}

// 后序遍历

public static void PostOrder(TreeNode root)

{

if (root == null)

return;

PostOrder(root.left);

PostOrder(root.right);

System.out.print(root.val+" ");

}

}

/* 运行结果:

先序遍历:

1 2 4 5 3 6

中序遍历:

4 2 5 1 6 3

后序遍历:

4 5 2 6 3 1

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Java实现二叉树输入先序遍历中序遍历然后输出后序遍历的代码: ```java public class BinaryTree { private TreeNode root; public BinaryTree() { this.root = null; } public void buildTree(int[] preorder, int[] inorder) { this.root = 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 inorderIndex = inStart; while (inorderIndex <= inEnd && inorder[inorderIndex] != rootVal) { inorderIndex++; } int leftSize = inorderIndex - inStart; root.left = buildTreeHelper(preorder, preStart + 1, preStart + leftSize, inorder, inStart, inorderIndex - 1); root.right = buildTreeHelper(preorder, preStart + leftSize + 1, preEnd, inorder, inorderIndex + 1, inEnd); return root; } public void printPostorder() { printPostorderHelper(root); } private void printPostorderHelper(TreeNode node) { if (node == null) { return; } printPostorderHelper(node.left); printPostorderHelper(node.right); System.out.print(node.val + " "); } private static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; this.left = null; this.right = null; } } public static void main(String[] args) { int[] preorder = {1, 2, 4, 5, 3, 6}; int[] inorder = {4, 2, 5, 1, 3, 6}; BinaryTree tree = new BinaryTree(); tree.buildTree(preorder, inorder); tree.printPostorder(); // Output: 4 5 2 6 3 1 } } ``` 这个实现中,我们首先定义了一个内部类 `TreeNode` 表示二叉树的节点。然后,我们定义了一个 `buildTree` 方法来构建二叉树。该方法接受先序遍历中序遍历数组,并返回二叉树的根节点。 在 `buildTree` 方法中,我们首先获取先序遍历的第一个元素作为根节点,然后在中序遍历中找到根节点的位置。我们可以用这个位置来计算左子树和右子树的大小,并递归构建左子树和右子树。 最后,我们定义了一个 `printPostorder` 方法来输出后序遍历。该方法只是调用了一个辅助方法 `printPostorderHelper`,该方法使用递归方式遍历二叉树,并输出每个节点的值。 最后,我们在 `main` 方法中创建了一个二叉树,并输出了它的后序遍历结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值