根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3
/ \
9 20
/ \
15 7
请先翻阅 LeetCode 从前序与中序遍历序列构造二叉树
此题与上一题基本一样的解法。
中序遍历:先左子树,后根节点,再右子树
后序遍历:先左子树,后右子树,再根节点
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int orderSize;
vector<int> inorder;//类的属性,作用类似全局遍历
vector<int> postorder;
//inorder [inorderBegin, inorderEnd], postorder [postorderBegin, postorderEnd] 构造成一棵树
TreeNod