Leetcode::Construct Binary Tree from Inorder and Postorder Traversal
2013-07-01 08:39
411 查看
/** * 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 *build( vector<int>&inorder, vector<int> &postorder,int in_s,int in_e, int pos_s, int pos_e) { if( in_s >= in_e ) { return NULL; } int val = postorder[pos_e-1]; TreeNode * tmp=new TreeNode(val); int i; for(i=in_s; i< in_e ; i++ ) { if( val == inorder[i]) { break; } } int len = i - in_s; tmp->left=build(inorder,postorder,in_s,i,pos_s ,pos_s + len); tmp->right=build(inorder,postorder,i+1,in_e,pos_s+len,pos_e -1); return tmp; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { // Start typing your C/C++ solution below // DO NOT write int main() function return build(inorder,postorder,0,inorder.size(),0,postorder.size()); } };
相关文章推荐
- 【leetcode】106. Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
- LeetCode-106. Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- [面试真题] LeetCode:Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)
- 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 020: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode: Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal