Construct Binary Tree from Inorder and Postorder Traversa
来源:互联网 发布:金融数据分析师好学么? 编辑:程序博客网 时间:2024/06/13 08:18
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution { int findpos(vector<int> &vec,int s,int e,int val){ for(int i=s;i<=e;++i){ if(vec[i]==val)return i; } return -1; } TreeNode *Build(vector<int> &inorder,int si,int ei, vector<int> &postorder,int sp,int ep){ int val=postorder[ep]; TreeNode *root = new TreeNode(val); int it= findpos(inorder,si,ei,val); if(si>it-1) root->left=NULL; else root->left = Build(inorder,si,it-1,postorder,sp,sp+(it-si)-1); if(it+1>ei) root->right=NULL; else root->right = Build(inorder,it+1,ei,postorder,ep-(ei-it),ep-1); return root; }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; return Build(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); }};
- Construct Binary Tree from Inorder and Postorder Traversa
- *(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 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
- 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
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- 使用Varnish代替Squid做网站缓存加速器的详细解决方案
- varnish后端的备份
- Ubuntu下使用SVN
- Eclipse与Android源码中ProGuard工具的使用
- varnishd 配置及其使用
- Construct Binary Tree from Inorder and Postorder Traversa
- 微软产品XNA开发工具
- 手把手让你实现开源企业级web高并发解决方案(lvs+heartbeat+varnish+nginx+eAccelerator+memcached)
- typedef的用法以及与define 的区别
- chroot备忘
- VC++笔记七
- Oracle 删除用户和表空间
- Java 中的内部类
- Linux内核模块与Makefile实例