本文共 1120 字,大约阅读时间需要 3 分钟。
C++
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: struct TreeNode* reConstructBinaryTree(vector pre,vector in) { if (pre.size() == 0) { return NULL; } TreeNode *root = new TreeNode(pre[0]); vector l_in, r_in, l_pre, r_pre; bool flag = true; for (int i = 0; i < pre.size(); i++) { if (in[i] == pre[0]) { flag = false; continue; } if (flag == true) { l_pre.push_back(pre[i+1]); l_in.push_back(in[i]); } else { r_pre.push_back(pre[i]); r_in.push_back(in[i]);; } } root->left = reConstructBinaryTree(l_pre, l_in); root->right = reConstructBinaryTree(r_pre, r_in); return root; }};
本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5112653.html,如需转载请自行联系原作者