通过前序遍历和中序遍历重建二叉树以及输出后序遍历(Java实现)(二)

前面写过一篇用数组表示的二叉树的重建,这篇补上用TreeNode连接起来的树的重建。

/*
 * 使用节点类实现树的重建以及各种遍历
 * 实现给定一个节点将该树镜像化的方法
 */
package sh.tree;

public class RebuildTreeTest {

	public static void main(String[] args) {
		RebuildTreeTest t = new RebuildTreeTest();
		int[] qx = { 1, 2, 4, 7, 3, 5, 6, 8 };
		int[] zx = { 4, 7, 2, 1, 5, 3, 8, 6 };
		TreeNode head = t.build(qx, zx);
		System.out.print("前序遍历为:");
		t.qxbl(head);
		System.out.println();
		System.out.print("中序遍历为:");
		t.zxbl(head);
		System.out.println();
		System.out.print("后序遍历为:");
		t.hxbl(head);
		System.out.println("\n*********镜像化**********");
		t.mirrorize(head);
		System.out.print("前序遍历为:");
		t.qxbl(head);
		System.out.println();
		System.out.print("中序遍历为:");
		t.zxbl(head);
		System.out.println();
		System.out.print("后序遍历为:");
		t.hxbl(head);
		
	}

	// 重建树
	public TreeNode build(int[] qx, int[] zx) {
		TreeNode head = null;
		if (qx.length > 0) {
			head = new TreeNode(qx[0]);
		}
		if (qx.length == 1 || zx.length == 1) {
			return head;
		}

		// 找到在中序遍历中的根节点位置
		int len = qx.length;
		int root = -1;
		for (int i = 0; i < len; i++) {
			if (zx[i] == head.val) {
				root = i;
				break;
			}
		}
		
		// 将前序数组和中序数组分成以根节点为中心的两组数组
		int leftqx[] = new int[root];
		int rightqx[] = new int[len - 1 - root];
		int leftzx[] = new int[root];
		int rightzx[] = new int[len - 1 - root];
		for (int j = 0; j < len; j++) {
			// 左边前序和右边前序赋值
			if (j < root)
				leftqx[j] = qx[j + 1];
			else if (j < len - 1)
				rightqx[j - root] = qx[j + 1];
			// 左边中序和右边中序
			if (head.val != zx[j]) {
				if (j < root)
					leftzx[j] = zx[j];
				else
					rightzx[j - root - 1] = zx[j];
			}
		}
		
		//根据是左边还是右边分别将其设置为左子树和右子树
		if (leftqx.length > 0 || leftzx.length > 0)
			head.left = build(leftqx, leftzx);
		if (rightqx.length > 0 || rightzx.length > 0)
			head.right = build(rightqx, rightzx);
		return head;
	}

	public void mirrorize(TreeNode root){

		if(root == null)
			return;
		
		TreeNode leftChild = root.left;
		TreeNode rightChild = root.right;
		
		if(leftChild != null || rightChild != null){
			root.left = rightChild;
			root.right = leftChild;
			mirrorize(leftChild);
			mirrorize(rightChild);
		}
		
	}
	
	// 树的前序遍历
	public void qxbl(TreeNode head) {
		// 先输出根节点再输出子节点
		if (head != null)
			System.out.print(head.val + " ");
		if (head.left != null) {
			qxbl(head.left);
		}
		if (head.right != null) {
			qxbl(head.right);
		}
	}

	// 中序遍历
	public void zxbl(TreeNode head) {
		if (head == null)
			return;
		if (head.left != null)
			zxbl(head.left);
		System.out.print(head.val + " ");
		if (head.right != null)
			zxbl(head.right);
	}

	// 后序遍历
	public void hxbl(TreeNode head) {
		if(head == null)
			return;
		if(head.left != null)
			hxbl(head.left);
		if(head.right != null)
			hxbl(head.right);
		System.out.print(head.val +" ");
	}
}

树的定义:

package sh.tree;

public class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) { val = x; }
}
输出

前序遍历为:1 2 4 7 3 5 6 8 
中序遍历为:4 7 2 1 5 3 8 6 
后序遍历为:7 4 2 5 8 6 3 1 
*********镜像化**********
前序遍历为:1 3 6 8 5 2 4 7 
中序遍历为:6 8 3 5 1 2 7 4 
后序遍历为:8 6 5 3 7 4 2 1 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值