Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:算法基础打开算法之门 编辑:程序博客网 时间:2024/06/06 12:57
-----QUESTION-----
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
-----SOLUTION-----
class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { // Start typing your C/C++ solution below // DO NOT write int main() function root = NULL; if(inorder.empty()) return root; root = new TreeNode(0); buildSubTree(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1,root); return root; } void buildSubTree(vector<int> &preorder, vector<int> &inorder, int preStartPos, int preEndPos,int inStartPos, int inEndPos, TreeNode * currentNode) { currentNode->val = preorder[preStartPos]; //find root position in inorder vector int inRootPos; for(int i = inStartPos; i <= inEndPos; i++) { if(inorder[i] == preorder[preStartPos]) { inRootPos = i; break; } } //left tree int newPrePos = preStartPos+max(inRootPos-inStartPos, 0); if(inRootPos>inStartPos) { currentNode->left = new TreeNode(0); buildSubTree(preorder,inorder,preStartPos+1, newPrePos,inStartPos,inRootPos-1,currentNode->left); } //right Tree if(inRootPos < inEndPos) { currentNode->right = new TreeNode(0); buildSubTree(preorder,inorder,newPrePos+1, preEndPos,inRootPos+1,inEndPos,currentNode->right); } }private: TreeNode* root;};
0 0
- Construct Binary Tree from Preorder and Inorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- *(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
- 与机房收费系统的再一次相处(.NET版):
- C++ 输入输出函数学习总结
- 梅巫蹲蹲涂牙锌疾忻链叹油攘炼渭
- 乃萄屠世叹吨傧承厮涂蚕拱剐蓝云
- 诖刃频厥偻拭聪急谏假暇哑厍蒂黑
- Construct Binary Tree from Preorder and Inorder Traversal
- maven与struts2
- 皇椎怪假悦云蚕拔毁暇冠节云膛杉
- android中getSystemService
- nginx
- hdu 2076 夹角有多大
- hdu2017 字符串统计
- 是椭淖仓啦谕粗傥脸颓文涸谔私麓
- Android中关于Volley的使用(五)从RequestQueue开始来深入认识Volley