Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:淘宝联盟佣金设置 编辑:程序博客网 时间:2024/06/01 13:30
/** * 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> &preorder,int sp,int ep){ int val=preorder[sp]; 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,preorder,sp+1,sp+(it-si)); if(it+1>ei) root->right=NULL; else root->right = Build(inorder,it+1,ei,preorder,ep-(ei-it)+1,ep); return root; }public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { // 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,preorder,0,preorder.size()-1); }};
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal & Construct Binary Tree from Inorder and P
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- 亲爱的你,我拿什么来爱你?
- HDU 4512 吉哥系列故事——完美队形I
- Mysql,主从数据同步的搭建步骤
- SpringMVC + Spring + SpringJDBC整合
- windows 下的svn服务端与客户端的搭建与使用
- Construct Binary Tree from Preorder and Inorder Traversal
- ubuntu默认root密码
- uva 539 The Settlers of Catan
- Oracle 中获取日期中的某部分值
- POJ 1002 487-3279(排序or不排序)
- Activity设置无标题栏效果和全屏效果
- NSNotificationCenter postNotificationName同步阻塞调用
- 游戏内实现手机摇一摇分析
- dedecms 调用多个栏目的用法