首页 > 其他 > 详细

剑指 Offer 07. 重建二叉树

时间:2020-06-28 23:06:58      阅读:65      评论:0      收藏:0      [点我收藏+]
import java.util.LinkedList;

/**
 * @Class BuildTree
 * @Description 剑指 Offer 07. 重建二叉树
 * 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。
 * 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
 * <p>
 * 例如,给出
 * 前序遍历 preorder = [3,9,[10,11],20,15,7]
 * 中序遍历 inorder = [10,9,11,3,15,20,7]
 * 返回如下的二叉树:
 * 3
 * /    * 9    20
 * / \   /   * 10 11 15 7
 * <p>
 * 限制:
 * 0 <= 节点个数 <= 5000
 * @Author
 * @Date 2020/6/28
 **/
public class BuildTree {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
            val = x;
        }
    }
}
// 利用递归
public static TreeNode buildTree(int[] preorder, int[] inorder) {
	if (preorder == null || inorder == null) {
		return null;
	}
	if (preorder.length == 1 && inorder.length == 1) {
		return new TreeNode(preorder[0]);
	}
	TreeNode treeNode = buildTree(preorder, 0, inorder, 0, inorder.length - 1);
	return treeNode;
}

private static TreeNode buildTree(int[] preorder, int rootIndex, int[] inorder, int start, int end) {
	// 终止条件
	if (preorder == null || inorder == null ||
			start > end || start >= inorder.length) {
		return null;
	}
	int index = start;
	for (; index <= end; index++) {
		if (inorder[index] == preorder[rootIndex]) break;
	}
	TreeNode treeNode = new TreeNode(preorder[rootIndex]);
	// rootIndex++ 会导致递归内存溢出,x++与++x的区别
	// 左子树的范围是inorder[start,index-1];,左子节点是preorder[++rootIndex]
	// 右子树的范围是inorder[index+1,end];右子节点是preorder[rootIndex + index - start]
	treeNode.left = buildTree(preorder, ++rootIndex, inorder, start, index - 1);
	treeNode.right = buildTree(preorder, rootIndex + index - start, inorder, index + 1, end);
	return treeNode;
}
/**
 * 解法2:利用递归
 */
public static int[] reversePrint(ListNode head) {
	LinkedList<Integer> linkedList = new LinkedList<>();
	reversePrint(head,linkedList);
	int size = linkedList.size();
	int[] ans = new int[size];
	for (int i = 0; i < size; i++) {
		ans[i]= linkedList.pop();
	}
	return ans;
}

private static LinkedList<Integer> reversePrint(ListNode listNode, LinkedList linkedList){
	if(listNode==null) return null;
	linkedList.add(listNode.val);
	return reversePrint(listNode.next,linkedList);
}
// 测试用例
public static void print(TreeNode treeNode, LinkedList linkedList) {
	if (treeNode == null) return;
	print(treeNode.left, linkedList);
	print(treeNode.right, linkedList);
	linkedList.add(treeNode.val);
}
public static void main(String[] args) {
	int[] preorder = new int[]{3, 9, 20, 15, 7};
	int[] inorder = new int[]{9, 3, 15, 20, 7};
	TreeNode treeNode = buildTree(preorder, inorder);
	LinkedList linkedList = new LinkedList();
	print(treeNode, linkedList);
	System.out.println("demo01 result:");
	for (int i = 0; i < linkedList.size(); i++) {
		System.out.print(" " + linkedList.get(i));
	}
	System.out.println("");

	preorder = new int[]{1,2,3};
	inorder = new int[]{3,2,1};
	treeNode = buildTree(preorder, inorder);
	linkedList = new LinkedList();
	print(treeNode, linkedList);
	System.out.println("demo02 result:");
	for (int i = 0; i < linkedList.size(); i++) {
		System.out.print(" " + linkedList.get(i));
	}
	System.out.println("");
}

剑指 Offer 07. 重建二叉树

原文:https://www.cnblogs.com/fyusac/p/13205269.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!