从中序与后序遍历序列构造二叉树
1.题目内容
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
示例 1:

1 2
| 输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] 输出:[3,9,20,null,null,15,7]
|
示例 2:
1 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 保证是树的后序遍历
2.解法
(1)递归
思路及算法
首先解决这道题我们需要明确给定一棵二叉树,我们是如何对其进行中序遍历与后序遍历的:
- 中序遍历的顺序是每次遍历左孩子,再遍历根节点,最后遍历右孩子。
- 后序遍历的顺序是每次遍历左孩子,再遍历右孩子,最后遍历根节点。
因此根据上文所述,我们可以发现后序遍历的数组最后一个元素代表的即为根节点。知道这个性质后,我们可以利用已知的根节点信息在中序遍历的数组中找到根节点所在的下标,然后根据其将中序遍历的数组分成左右两部分,左边部分即左子树,右边部分为右子树,针对每个部分可以用同样的方法继续递归下去构造。

算法过程如下:
为了高效查找根节点元素在中序遍历数组中的下标,我们选择创建哈希表来存储中序序列,即建立一个(元素,下标)键值对的哈希表。
定义递归函数 helper(in_left, in_right) 表示当前递归到中序序列中当前子树的左右边界,递归入口为helper(0, n - 1) :
- 如果 in_left > in_right,说明子树为空,返回空节点。
- 选择后序遍历的最后一个节点作为根节点。
- 利用哈希表 O(1) 查询当根节点在中序遍历中下标为 index。从 in_left 到 index - 1 属于左子树,从 index + 1 到 in_right 属于右子树。
- 根据后序遍历逻辑,递归创建右子树 helper(index + 1, in_right) 和左子树 helper(in_left, index - 1)。注意这里有需要先创建右子树,再创建左子树的依赖关系。可以理解为在后序遍历的数组中整个数组是先存储左子树的节点,再存储右子树的节点,最后存储根节点,如果按每次选择「后序遍历的最后一个节点」为根节点,则先被构造出来的应该为右子树。
- 返回根节点 root。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| //Java class Solution { int post_idx; int[] postorder; int[] inorder; Map<Integer, Integer> idx_map = new HashMap<Integer, Integer>();
public TreeNode helper(int in_left, int in_right) { // 如果这里没有节点构造二叉树了,就结束 if (in_left > in_right) { return null; }
// 选择 post_idx 位置的元素作为当前子树根节点 int root_val = postorder[post_idx]; TreeNode root = new TreeNode(root_val);
// 根据 root 所在位置分成左右两棵子树 int index = idx_map.get(root_val);
// 下标减一 post_idx--; // 构造右子树 root.right = helper(index + 1, in_right); // 构造左子树 root.left = helper(in_left, index - 1); return root; }
public TreeNode buildTree(int[] inorder, int[] postorder) { this.postorder = postorder; this.inorder = inorder; // 从后序遍历的最后一个元素开始 post_idx = postorder.length - 1;
// 建立(元素,下标)键值对的哈希表 int idx = 0; for (Integer val : inorder) { idx_map.put(val, idx++); } return helper(0, inorder.length - 1); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| //C++ class Solution { int post_idx; unordered_map<int, int> idx_map; public: TreeNode* helper(int in_left, int in_right, vector<int>& inorder, vector<int>& postorder){ // 如果这里没有节点构造二叉树了,就结束 if (in_left > in_right) { return nullptr; }
// 选择 post_idx 位置的元素作为当前子树根节点 int root_val = postorder[post_idx]; TreeNode* root = new TreeNode(root_val);
// 根据 root 所在位置分成左右两棵子树 int index = idx_map[root_val];
// 下标减一 post_idx--; // 构造右子树 root->right = helper(index + 1, in_right, inorder, postorder); // 构造左子树 root->left = helper(in_left, index - 1, inorder, postorder); return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { // 从后序遍历的最后一个元素开始 post_idx = (int)postorder.size() - 1;
// 建立(元素,下标)键值对的哈希表 int idx = 0; for (auto& val : inorder) { idx_map[val] = idx++; } return helper(0, (int)inorder.size() - 1, inorder, postorder); } };
|
复杂度分析
(2)迭代
思路及算法
迭代法是一种非常巧妙的实现方法。迭代法的实现基于以下两点发现。
- 如果将中序遍历反序,则得到反向的中序遍历,即每次遍历右孩子,再遍历根节点,最后遍历左孩子。
- 如果将后序遍历反序,则得到反向的前序遍历,即每次遍历根节点,再遍历右孩子,最后遍历左孩子。
- 「反向」的意思是交换遍历左孩子和右孩子的顺序,即反向的遍历中,右孩子在左孩子之前被遍历。
对于后序遍历中的任意两个连续节点 u 和 v(在后序遍历中,u 在 v 的前面),根据后序遍历的流程,我们可以知道 u 和 v 只有两种可能的关系:
- u 是 v 的右儿子。这是因为在遍历到 u 之后,下一个遍历的节点就是 u 的双亲节点,即 v;
- v 没有右儿子,并且 u 是 v 的某个祖先节点(或者 v 本身)的左儿子。如果 v 没有右儿子,那么上一个遍历的节点就是 v 的左儿子。如果 v 没有左儿子,则从 v 开始向上遍历 v 的祖先节点,直到遇到一个有左儿子(且 v 不在它的左儿子的子树中)的节点 va,那么 u 就是 va 的左儿子。
算法流程:
- 我们用一个栈和一个指针辅助进行二叉树的构造。初始时栈中存放了根节点(后序遍历的最后一个节点),指针指向中序遍历的最后一个节点;
- 我们依次枚举后序遍历中除了第一个节点以外的每个节点。如果 index 恰好指向栈顶节点,那么我们不断地弹出栈顶节点并向左移动 index,并将当前节点作为最后一个弹出的节点的左儿子;如果 index 和栈顶节点不同,我们将当前节点作为栈顶节点的右儿子;
- 无论是哪一种情况,我们最后都将当前的节点入栈。
最后得到的二叉树即为答案。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| //Java class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { if (postorder == null || postorder.length == 0) { return null; } TreeNode root = new TreeNode(postorder[postorder.length - 1]); Deque<TreeNode> stack = new LinkedList<TreeNode>(); stack.push(root); int inorderIndex = inorder.length - 1; for (int i = postorder.length - 2; i >= 0; i--) { int postorderVal = postorder[i]; TreeNode node = stack.peek(); if (node.val != inorder[inorderIndex]) { node.right = new TreeNode(postorderVal); stack.push(node.right); } else { while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) { node = stack.pop(); inorderIndex--; } node.left = new TreeNode(postorderVal); stack.push(node.left); } } return root; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| //C++ class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if (postorder.size() == 0) { return nullptr; } auto root = new TreeNode(postorder[postorder.size() - 1]); auto s = stack<TreeNode*>(); s.push(root); int inorderIndex = inorder.size() - 1; for (int i = int(postorder.size()) - 2; i >= 0; i--) { int postorderVal = postorder[i]; auto node = s.top(); if (node->val != inorder[inorderIndex]) { node->right = new TreeNode(postorderVal); s.push(node->right); } else { while (!s.empty() && s.top()->val == inorder[inorderIndex]) { node = s.top(); s.pop(); inorderIndex--; } node->left = new TreeNode(postorderVal); s.push(node->left); } } return root; } };
|
复杂度分析