LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:零基础学算法 第二版 编辑:程序博客网 时间:2024/06/04 19:16
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Code:
<span style="font-size:14px;">/** * 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 *helper(const vector<int> &inorder, const vector<int> &postorder, int inBegin, int inEnd, int postBegin, int postEnd) { if (inBegin > inEnd) return NULL; TreeNode *root = new TreeNode(postorder[postEnd]); int index = inBegin; for (; index <= inEnd; ++index) if (inorder[index] == postorder[postEnd]) break; root->left = helper(inorder, postorder, inBegin, index-1, postBegin, index-1-inBegin+postBegin); root->right = helper(inorder, postorder, index+1, inEnd, index-inBegin+postBegin, postEnd-1); return root; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { return helper(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1); }};</span>
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
- php Warning:date(): It is not safe to rely on the system's timezone settings
- 从抽象谈起(二):观察者模式与回调
- highcharts spline传入数据
- C# Socket异步通讯实现详解
- Cesium WebGL 调试环境配置
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- 汽车VIN号所标示的意义是什么
- Ajax:
- Mybatis_review之配置文件中的typeHandlers节点内容说明
- 网络流—最大流(Edmond-Karp算法)
- 再遇MySQL 启动 1067 错误。
- hdu 1548 A strange lift
- Scala新手指南中文版 - 第一篇 Extractors(提取器)
- Scala新手指南中文版 - 第二篇 Extracting Sequences(提取有序类型)