标签:bin str 二叉树 tree int 就是 return roo color
题目描述
注意:举例子进行编程,注意边界情况,还有就是注意不要忘记递归基。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { if(pre.size() != vin.size()){ return nullptr; } if(pre.size() == 0){//递归基 return nullptr; } TreeNode* root = new TreeNode(pre[0]); vector<int> vin_left,vin_right,pre_left,pre_right; int pos = 0; for(pos;pos < vin.size();++pos){ if(vin[pos] == root -> val){ break; } vin_left.push_back(vin[pos]); pre_left.push_back(pre[pos + 1]); } pos++; for(pos;pos < vin.size();++pos){ vin_right.push_back(vin[pos]); pre_right.push_back(pre[pos]); } root -> left = reConstructBinaryTree(pre_left,vin_left); root -> right = reConstructBinaryTree(pre_right,vin_right); return root; } };
标签:bin str 二叉树 tree int 就是 return roo color
原文地址:http://www.cnblogs.com/dingxiaoqiang/p/7881604.html