标签:efi ini wrap ++ white ace tor roo rest
1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) { 13 int n = pre.size();//确定节点数目 14 if(n <= 0) 15 return NULL; 16 else 17 return reConstructBinaryTree2(pre,0,n-1,in,0,n-1); 18 } 19 TreeNode* reConstructBinaryTree2(vector<int> pre,int preStart,int preEnd,vector<int> in,int inStart,int inEnd) 20 { 21 if(preStart > preEnd || inStart > inEnd) 22 return NULL; 23 TreeNode *root = new TreeNode(pre[preStart]); 24 for(int i = inStart; i <= inEnd; i++) 25 { 26 if(in[i] == pre[preStart])//在中序遍历中找到根节点 27 { 28 root->left = reConstructBinaryTree2(pre,preStart+1,preStart+i-inStart,in,inStart,i-1); 29 root->right = reConstructBinaryTree2(pre,preStart+i-inStart+1,preEnd,in,i+1,inEnd); 30 } 31 } 32 return root; 33 34 } 35 };
标签:efi ini wrap ++ white ace tor roo rest
原文地址:http://www.cnblogs.com/qqky/p/6801630.html