题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
解题思路:在二叉树的前序遍历中,根节点总是最先出现;而中序遍历中,左子树的节点先出现。因此,我们可以先根据前序遍历中的第一个值k,构建根节点,接着在中序遍历中找到k, 则中序遍历中k左边的为左子树,右边的为右子树,然后递归的构建左右子树即可。
Python 代码
1 2 3 4 5 6 7 8
| def reConstructBinaryTree(self, pre, tin): if len(pre) == 0 or len(tin) == 0: return None idx = tin.index(pre[0]) root = TreeNode(pre.pop(0)) root.left = self.reConstructBinaryTree(pre, tin[:idx]) root.right = self.reConstructBinaryTree(pre, tin[idx+1:]) return root
|
使用c++实现中,由于给出的vector不能pop出第0个值,因此,先反转顺序。
c++ 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| TreeNode* reConstructBinaryTreeHelper(vector<int> &pre,vector<int> vin) { if (pre.empty() || vin.empty()) return NULL; TreeNode* tmp = new TreeNode(pre.back()); int i=0; for (; i<vin.size(); i++) { if (vin[i] == pre.back()) break; } pre.pop_back(); tmp->left = reConstructBinaryTreeHelper(pre, {vin.begin(), vin.begin()+i}); if (vin.size() == 1) tmp->right = NULL; else tmp->right = reConstructBinaryTreeHelper(pre, {vin.begin()+i+1, vin.end()}); return tmp; } TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { vector<int> revpre; while (!pre.empty()) { revpre.push_back(pre.back()); pre.pop_back(); } return reConstructBinaryTreeHelper(revpre, vin); }
|