Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:编译单个java文件 编辑:程序博客网 时间:2024/05/16 11:52
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
struct Node{int val;Node(int v){val = v;left = right = NULL;}Node *left;Node *right;};Node* visit(vector<int> &postorder, int postLeft, int postRight, vector<int> &inorder, int inLeft, int inRight){if (postLeft > postRight){return NULL;}int index = inLeft;for (;index <= inRight; index++){if (inorder[index] == postorder[postRight]){break;}}int leftLength = index - inLeft;Node *left = visit(postorder, postLeft, postLeft+leftLength-1, inorder, inLeft, index-1);Node *right = visit(postorder, postLeft+leftLength, postRight-1, inorder, index+1, inRight);Node *p = new Node(postorder[postRight]);p->left = left;p->right = right;return p;}Node* buildTree(vector<int> &postorder, vector<int> &inorder){if (postorder.size() < 1){return NULL;}return visit(postorder, 0, postorder.size()-1, inorder, 0, inorder.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
- 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
- 【寒假任务】 洛谷1051 谁拿了最多奖学金
- Nexus 私服上传文件,并通过 Gradle 引用
- HDU-2055(水题)
- ASP.NET开发实战精品源码及视频教程下载
- 【Android Library】项目导入到Nexus私服
- Construct Binary Tree from Inorder and Postorder Traversal
- 编译及调试OpenJDK 8
- 贝叶斯分类器
- ResourceBundle的MissingResourceException异常问题
- 【牛腩新闻发布系统】无法建立与SQL的链接
- 数学建模算法总结(三)
- 对于url href src的一点想法
- HDU - 2600 War 解题报告
- poj 3984 迷宫问题