先序、中序和后序数组两两结合重构二叉树 -- 图解

先序、中序和后序数组两两结合重构二叉树

 

 

package chapter_3_binarytreeproblem;

import java.util.HashMap;

public class Problem_21_PreInPosArrayToTree {

	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}

	public static Node preInToTree(int[] pre, int[] in) {
		if (pre == null || in == null) {
			return null;
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < in.length; i++) {
			map.put(in[i], i);
		}
		return preIn(pre, 0, pre.length - 1, in, 0, in.length - 1, map);
	}

	public static Node preIn(int[] p, int pi, int pj, int[] n, int ni, int nj,
			HashMap<Integer, Integer> map) {
		if (pi > pj) {
			return null;
		}
		Node head = new Node(p[pi]);
		int index = map.get(p[pi]);
		head.left = preIn(p, pi + 1, pi + index - ni, n, ni, index - 1, map);
		head.right = preIn(p, pi + index - ni + 1, pj, n, index + 1, nj, map);
		return head;
	}

	public static Node inPosToTree(int[] in, int[] pos) {
		if (in == null || pos == null) {
			return null;
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < in.length; i++) {
			map.put(in[i], i);
		}
		return inPos(in, 0, in.length - 1, pos, 0, pos.length - 1, map);
	}

	public static Node inPos(int[] n, int ni, int nj, int[] s, int si, int sj,
			HashMap<Integer, Integer> map) {
		if (si > sj) {
			return null;
		}
		Node head = new Node(s[sj]);
		int index = map.get(s[sj]);
		head.left = inPos(n, ni, index - 1, s, si, si + index - ni - 1, map);
		head.right = inPos(n, index + 1, nj, s, si + index - ni, sj - 1, map);
		return head;
	}

	// 每个节点的孩子数都为0或2的二叉树才能被先序与后序重构出来
	public static Node prePosToTree(int[] pre, int[] pos) {
		if (pre == null || pos == null) {
			return null;
		}
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < pos.length; i++) {
			map.put(pos[i], i);
		}
		return prePos(pre, 0, pre.length - 1, pos, 0, pos.length - 1, map);
	}

	public static Node prePos(int[] p, int pi, int pj, int[] s, int si, int sj,
			HashMap<Integer, Integer> map) {
		Node head = new Node(s[sj--]);
		if (pi == pj) {
			return head;
		}
		int index = map.get(p[++pi]);
		head.left = prePos(p, pi, pi + index - si, s, si, index, map);
		head.right = prePos(p, pi + index - si + 1, pj, s, index + 1, sj, map);
		return head;
	}

	// for test -- print tree
	public static void printTree(Node head) {
		System.out.println("Binary Tree:");
		printInOrder(head, 0, "H", 17);
		System.out.println();
	}

	public static void printInOrder(Node head, int height, String to, int len) {
		if (head == null) {
			return;
		}
		printInOrder(head.right, height + 1, "v", len);
		String val = to + head.value + to;
		int lenM = val.length();
		int lenL = (len - lenM) / 2;
		int lenR = len - lenM - lenL;
		val = getSpace(lenL) + val + getSpace(lenR);
		System.out.println(getSpace(height * len) + val);
		printInOrder(head.left, height + 1, "^", len);
	}

	public static String getSpace(int num) {
		String space = " ";
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < num; i++) {
			buf.append(space);
		}
		return buf.toString();
	}

	public static void main(String[] args) {
		int[] pre = { 1, 2, 4, 5, 8, 9, 3, 6, 7 };
		int[] in = { 4, 2, 8, 5, 9, 1, 6, 3, 7 };
		int[] pos = { 4, 8, 9, 5, 2, 6, 7, 3, 1 };
		printTree(preInToTree(pre, in));
		printTree(inPosToTree(in, pos));
		printTree(prePosToTree(pre, pos));

	}

}

参考资料:

【1】程序员代码面试指南  -- 左程云

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值