-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#104.cc
35 lines (35 loc) · 1.06 KB
/
LeetCode#104.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int ind ;
void dfs(TreeNode*& root, int left, int right,
vector<int>& inorder,vector<int>& postorder){
if(left>right) return ;
int k ;
for(k=left;k<=right;k++)
if(inorder[k]==postorder[ind]) break;
ind--;
root = new TreeNode(inorder[k]);
dfs(root->right,k+1,right,inorder,postorder);
dfs(root->left,left,k-1,inorder,postorder);
return ;
}
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(inorder.size()==0) return NULL;
ind = inorder.size()-1;
TreeNode* ret = NULL;
dfs(ret,0,ind,inorder,postorder);
return ret;
}
};