根据中序遍历和后序遍历树构造二叉树
注意事项
你可以假设树中不存在相同数值的节点
样例
给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2]
返回如下的树:
2
/ \
1 3
思路
递归地构造二叉树,postorder的最后一个元素作为根节点rootVal,在inorder中寻找rootVal,其左右两边作为左右子树的inorder,并由此获得新子树的postorder。
#ifndef C72_H
#define C72_H
#include<iostream>
#include<vector>
using namespace std;
class TreeNode{
public:
int val;
TreeNode *left,