[leetcode] Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:jsp获取mac 编辑:程序博客网 时间:2024/06/10 02:08
Construct Binary Tree from Inorder and Postorder Traversal
class Solution {public: TreeNode *buildTreebyIndex(vector<int> &inorder,int inBegin,int inEnd,vector<int> &postorder,int postBegin,int postEnd){ if (inBegin>inEnd||postBegin>postEnd) { return nullptr; } int val=postorder[postEnd]; int index; for (int i=inBegin; i<=inEnd; i++) {//注意条件为<= if (inorder[i]==val) { index=i; break; } } int len=index-inBegin; TreeNode *node=new TreeNode(val); TreeNode *pleft=buildTreebyIndex(inorder, inBegin, index-1, postorder, postBegin, postBegin+len-1); TreeNode *pright=buildTreebyIndex(inorder, index+1, inEnd, postorder, postBegin+len, postEnd-1); node->left=pleft; node->right=pright; return node; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { if (inorder.size()==0) { return nullptr; } return buildTreebyIndex(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); }};
0 0
- *(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
- [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
- [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
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- 如何使用eclipse进行hibernate 快速开发
- OpenCV_局部图像特征的提取与匹配_源代码
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- 考研复试机试题(2010)
- OpenCV——HoG特征
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- 浅谈鼠标滚轮事件
- 背景建模(一) Evaluation of Background Subtraction Techniques
- Swig将C++编译lib项目转换为C#可引用的dll[勘误后]
- 番茄学习方法
- 《Python网络编程基础》笔记(一)----底层网络
- C++primer读书笔记8-重载操作符
- WIN7x64+VS2010+OpenCV2.4.9配置笔记
- 《把时间当作朋友》经典语句