LeetCode 106 / 105 从中序与后序 / 前序与中序遍历序列构造二叉树

参考文章

LeetCode 106 从中序与后序遍历序列构造二叉树

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal


题意:

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

示例 1:

输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

示例 2:
输入:inorder = [-1], postorder = [-1]
输出:[-1]

提示:
1 <= inorder.length <= 3000
postorder.length == inorder.length
-3000 <= inorder[i], postorder[i] <= 3000
inorder 和 postorder 都由 不同 的值组成
postorder 中每一个值都在 inorder 中
inorder 保证是树的中序遍历
postorder 保证是树的后序遍历


思路:

由中序与后序遍历序列构造二叉树其实在数据结构课程中已经学过了,主要的思路由下图可以看出(图片转自代码随想录):
在这里插入图片描述
每次将当前后序遍历序列中的最后一个元素作为当前的节点的值,然后在中序遍历序列中寻找这个值,中序遍历中在这个值左边的值就是在当前节点的左子树中,在这个值右边的值就是在当前节点的右子树中。

思路是不难的,难点主要是代码实现。我们使用递归法来做这道题,在每层递归中将当前的中序遍历序列数组和后序遍历序列数组进行切割。注意:将数组进行切割需要先对中序遍历序列数组进行切割,再切割后序遍历序列数组! 这是只有获取当前节点的值后在中序遍历序列数组找到这个值,再切割中序遍历数组,我们才能知道在当前节点的左子树和右子树中的节点数各有多少个,然后来对后序遍历数组进行切割。另外,数组切割过程仍然需要注意遵循左闭右开的原则。 更多细节在代码中感受吧!

本题Java代码:

class Solution {
	private TreeNode build(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight) {
		if (inRight - inLeft < 1) return null;
		if (inRight - inLeft == 1) return new TreeNode(inorder[inLeft]);
		int rootVal = postorder[postRight - 1];
		TreeNode root = new TreeNode(rootVal);
		int rootIndex = 0;
		for (int i = inLeft; i < inRight; i++) {
			if (inorder[i] == rootVal) {
				rootIndex = i;
				break;
			}
		}
		root.left = build(inorder, inLeft, rootIndex, postorder, postLeft, postLeft + (rootIndex - inLeft));
		root.right = build(inorder, rootIndex + 1, inRight, postorder, postLeft + (rootIndex - inLeft), postRight - 1);
		return root;
	}

	public TreeNode buildTree(int[] inorder, int[] postorder) {
		return build(inorder, 0, inorder.length, postorder, 0, postorder.length);
	}
}

 

LeetCode 105 从前序与中序遍历序列构造二叉树

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal


题意:

给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

示例 1:

输入: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
输出: [3,9,20,null,null,15,7]

示例 2:
输入: preorder = [-1], inorder = [-1]
输出: [-1]

提示:
1 <= preorder.length <= 3000
inorder.length == preorder.length
-3000 <= preorder[i], inorder[i] <= 3000
preorder 和 inorder 均 无重复 元素
inorder 均出现在 preorder
preorder 保证 为二叉树的前序遍历序列
inorder 保证 为二叉树的中序遍历序列


思路:

这道题其实跟上一道题是一样的,只不过我们改为每次由前序遍历序列数组的第一个值来获取当前节点的值。

这里我们注意一下,能由前序与中序遍历序列或由中序与后序遍历序列来构造二叉树,那么能不能由前序与后序遍历序列来构造二叉树?前序和后序不能唯一确定一颗二叉树! 答案是不能的!没有中序遍历无法确定左右部分,无法进行分割。

本题Java代码:

class Solution {
	private TreeNode build(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {
		if (preRight - preLeft < 1) return null;
		if (preRight - preLeft == 1) return new TreeNode(preorder[preLeft]);
		int rootVal = preorder[preLeft];
		TreeNode root = new TreeNode(rootVal);
		int rootIndex = 0;
		for (int i = inLeft; i < inRight; i++) {
			if (inorder[i] == rootVal) {
				rootIndex = i;
				break;
			}
		}
		root.left = build(preorder, preLeft + 1, preLeft + 1 + (rootIndex - inLeft), inorder, inLeft, rootIndex);
		root.right = build(preorder, preLeft + 1 + (rootIndex - inLeft), preRight, inorder, rootIndex + 1, inRight);
		return root;
	}

	public TreeNode buildTree(int[] preorder, int[] inorder) {
		return build(preorder, 0, preorder.length, inorder, 0, inorder.length);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值