标签:turn 遍历 img 二叉树 solution nod image 图片 序列
class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) { int vinlen=vin.size(); if(vinlen==0) return NULL; vector<int> pre_left, pre_right, vin_left, vin_right; TreeNode* head = new TreeNode(pre[0]); int gen = 0; for(int i=0;i<vinlen;i++) { if(vin[i]==pre[0]) { gen=i; break; } } for(int i=0;i<gen;i++) { vin_left.push_back(vin[i]);//中序遍历左子树遍历序列 pre_left.push_back(pre[i+1]);//前序遍历左子树遍历序列 } for(int i=gen+1;i< vinlen;i++) { vin_right.push_back(vin[i]);//中序遍历右子树遍历序列 pre_right.push_back(pre[i]);//前序遍历右边子树遍历序列 } head->left = reConstructBinaryTree(pre_left, vin_left); head->right = reConstructBinaryTree(pre_right, vin_right); return head; } };
标签:turn 遍历 img 二叉树 solution nod image 图片 序列
原文地址:https://www.cnblogs.com/make-big-money/p/12359771.html