Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:大司马杂货铺淘宝店 编辑:程序博客网 时间:2024/05/16 13:05
Given preorder and inorder 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: template<typename Iter> TreeNode* make(Iter pFirst , Iter pLast , Iter iFirst , Iter iLast) { if(pFirst == pLast) return NULL; if(iFirst == iLast) return NULL; int val = *pFirst; auto iRoot = find(iFirst , iLast , val); TreeNode* root = new TreeNode(*iRoot); int leftSize = iRoot - iFirst; root->left = make(pFirst+1 , pFirst+leftSize+1 , iFirst , iRoot); root->right = make(pFirst+leftSize+1 , pLast , iRoot + 1 , iLast); return root; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { int size = inorder.size(); if(size == 0) return NULL; return make(preorder.begin() , preorder.end() , inorder.begin() , inorder.end()); }};
0 0
- *(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
- php模拟post提交数据
- 在vs2013中使用正则表达式查找和替换文本内容
- CentOS SVN安装与配置
- 数据库的CMD操作
- uva 350 Pseudo-Random Numbers
- Construct Binary Tree from Preorder and Inorder Traversal
- 第十七周项目5--玩日期时间(4)
- 七款开源写作/存档工具或共享资源
- Android ColorStateList 按钮 文字状态 变色
- StartActivity 和 StartActivityForResult
- 【Apache Mina2.0开发之一】搭建Apache Mina框架并实现Server与Client端的简单消息传递!
- 首页new wj v1
- typeid()的用法
- 高德地图SDK使用经验