【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:百度贴吧营销软件 编辑:程序博客网 时间:2024/06/04 18:03
原题:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
代码:
/** * 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 *buildTree(vector<int> &inorder, vector<int> &postorder) { if(inorder.empty()||postorder.empty()) return NULL; return make(postorder.begin(), postorder.end(), inorder.begin(), inorder.end()); } TreeNode *make(vector<int>::iterator pFirst, vector<int>::iterator pEnd, vector<int>::iterator iFirst, vector<int>::iterator iEnd){ if(pFirst==pEnd||iFirst==iEnd) return NULL; TreeNode *root = new TreeNode(*(pEnd-1)); vector<int>::iterator iRoot = find(iFirst, iEnd, root->val); int rightsize = iEnd-iRoot; root->left = make(pFirst, pEnd-rightsize, iFirst, iRoot); root->right = make(pEnd-rightsize-1, pEnd-1, iRoot+1, iEnd); return root; }};AC
0 0
- 【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- 【Leetcode长征系列】Construct Binary Tree from Preorder and Inorder 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: 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] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: 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-Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- 黑马程序员——高新技术
- iOS 内存优化
- java读写文本文件大全
- MappedByteBuffer小结
- 变年轻的养颜排毒美食
- 【Leetcode长征系列】Construct Binary Tree from Inorder and Postorder Traversal
- Nginx+tomcat配置集群负载均衡
- 少的发生地方
- 收到发阿斯蒂芬
- 巧用while循环避免执行除了异常处理的的另一个处理机制
- Oracle使用技巧及PL/SQL Developer配置 转载
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
- piwik - 图片跟踪api
- Linux操作系统中的时间