LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:python 字符串格式化 编辑:程序博客网 时间:2024/05/15 13:16
LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
原题
Given inorder and postorder traversal of a tree, construct the binary tree.
后序遍历和中序遍历建立数
分析
跟LeetCode—105类似,后序遍历从后往前,要先递归右子树再递归左子树。
代码
class Solution {private: TreeNode* helper(vector<int>&inorder, vector<int>&postorder, map<int, int>&index,int pstart,int pend,int instart,int inend) { if (instart > inend) return NULL; int rootval = postorder[pend]; int rootindex = index[rootval]; TreeNode*root = new TreeNode(rootval); root->right = helper(inorder, postorder, index, pend-inend+rootindex, pend - 1, rootindex + 1, inend); root->left = helper(inorder, postorder, index, pstart, pend - inend + rootindex-1, instart,rootindex-1); return root; }public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { map<int, int>index; for (int i = 0; i < inorder.size(); i++) { index[inorder[i]] = i; } return helper(inorder, postorder, index, 0, postorder.size() - 1, 0, inorder.size() - 1); }};
1 0
- LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106:Construct Binary Tree from Inorder and Postorder Traversal
- leetcode106~Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode106. Construct Binary Tree from Inorder and Postorder Traversal
- *(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
- CodeForces 560A Currency System in Geraldion
- mysql事务之提交读(Read Committed)
- mysql事务之可重复读(Repeated Read)
- 优化mysql数据库性能的十个参数
- web资源访问流程
- LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
- python之false和None
- 300.LeetCode Longest Increasing Subsequence(medium)[动态规划]
- Java温习
- CodeForces 459B Pashmak and Flowers
- java SE基础(Collection接口概述)
- Java内存分析工具——Jstat
- plsql developer连接数据库时出现ORA-01033错误的解决方法
- Collection--Map