106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:java异常有哪些分类 编辑:程序博客网 时间:2024/06/02 05:40
对于后序遍历来说,最后一个元素一定是根节点。然后在中序遍历中找的其未知,左边是左子树,右边是右子树。
左子树在后序遍历中的最后一个节点又是根节点,从而递归下去。
/** * Definition for a binary tree node. * 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) { if(postorder.empty()) return NULL; for(int i = 0; i < inorder.size();i++) { m[inorder[i]] = i; } reutrn build(inorder,0,inorder.size() - 1,postorder,0,postorder.size() - 1); } TreeNode* build(vector<int>& inorder,int s0,vector<int>& postorder,int s1,int e1) { if(s0 > e0 || s1 > e1) { return NULL; } TreeNode* root = new TreeNode(postorder[e1]); int mid = m[postorder[e1]]; int num = mid - s0; root->left = build(inorder,s0,mid - 1,postorder,s1,s1 + num - 1); root->right = build(inorder,s0,mid + 1,postorder,s1 + num + 1,e1 - 1); }};
0 0
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode --- 106. Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode] 106.Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode *** 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 实践---搭建mybatis框架
- UI——Material Design设计之Toolbar详解
- gerrit install plugin
- HDU 2087剪花布条
- VS2013打开2015的项目报错 The specified task executable location ... csc.exe" is invalid.
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 软件测试报告应该注意哪些事项
- spark点点滴滴 —— 开发运行scala程序
- 非农数据惊人利好 美联储6月加息概率大增,黄金的好日子到头了
- 博客之新增上传文件(包括图片)功能
- myeclipse安装activiti插件(不需要网络)
- jQuery同步Ajax带来的UI线程阻塞问题及解决办法
- filter与servlet的比较
- C++第六次实验-项目2