LeetCode Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:图片软件下载 编辑:程序博客网 时间:2024/04/27 10:35
Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
一个小小的下标没搞好,居然要浪费差不多一个小时,心痛啊。
中序遍历序列和后序遍历序列的偏移位置都需要仔细设计好。
class Solution {public:TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {return conTree(preorder, inorder, 0, preorder.size()-1, 0, inorder.size()-1);}TreeNode *conTree(vector<int> &preo, vector<int> &ino, int prel, int prer, int inl, int inr){if (prel > prer) return NULL;TreeNode *t = new TreeNode(preo[prel]);auto it = find(ino.begin()+inl, ino.begin()+inr+1, preo[prel]);int offset = it-ino.begin()-inl;//注意:切记,下标如何定位要清楚,是offset还是绝对位置不能搞混了!t->left = conTree(preo, ino, prel+1, prel+offset, inl, inl+offset-1);t->right = conTree(preo, ino, prel+offset+1, prer, inl+offset+1, inr);return t;}};
1 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
- 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 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
- [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
- 个人推荐的SIP software Phone
- ActiveMQ服务无法启动
- 最大报帐
- 点击单选按钮radio后面的文字即可选中单选按钮
- Javascript四舍五入(Math.round()与Math.pow())
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- CKEDITOR多实例问题的解决(转)
- SIP消息路由
- UINavigationcontrollerBar,UIbarButtonItem按钮以及backButton背景及背景颜色改变
- boost log库 使用八
- linux下杀死进程(kill)的N种方法 包括python--killproc
- 深入理解connect by level<xxx
- zend studio 10 字体,颜色,快捷键等相关设置
- 使用Net-SNMP监控同一服务的不同应用