Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:人工智能 军民融合 编辑:程序博客网 时间:2024/06/13 06:09
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
和上一篇一样,只不过用的是后序遍历。有一点在上一篇中忘记提出来了,vector.end()指针指向的并不是vector内的数。
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int size = inorder.size(); if(size==0) return NULL; return solve(inorder.begin(),inorder.end()-1,postorder.begin(),postorder.end()-1); } template <typename Iter> TreeNode *solve(Iter iFirst,Iter iLast,Iter pFirst,Iter pLast) { if(iFirst==iLast) return NULL; if(pFirst==pLast) return NULL; pLast--; Iter iRoot = find(iFirst,iLast,*pLast); TreeNode *root = new TreeNode(*iRoot); int size = iRoot - iFirst; root->left = solve(iFirst,iRoot,pFirst,pFirst+size); root->right = solve(iRoot+1,iLast,pFirst+size,pLast); return root; }};
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
- 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
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- opencv2鼠标选定感兴趣区域用反投影算法检测相似部分
- 如何处理超出 GL_MAX_TEXTURE_SIZE 的图片
- shell脚本抽取文本文件中指定字符串的方法:sed+grep方法、awk+grep方法(必要时可以联合sed以及grep)、grep+cut方法
- 信息系统项目管理师 下午论文应试策略
- Unity通过层级关系获取子物体
- Construct Binary Tree from Inorder and Postorder Traversal
- arcgis for js api导入eclipse错误
- Linux网络配置
- wikioi 1545 最简单的排序
- qt动态库与静态库编译、应用以及pri文件创建
- 深入探索透视投影变换
- UNIX环境高级编程-第9章- 进程关系
- soj 4389模拟
- jython语法