106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:手机淘宝2015旧版本5.4 编辑:程序博客网 时间:2024/05/17 04:18
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
思路:和上题类似。
class Solution {public: unordered_map<int,int> map; TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { if(postorder.empty()) return NULL; for(int i = 0; i < inorder.size(); i++) { map.insert(pair<int,int>(inorder[i],i)); } return buildTreeHelper(postorder,0,postorder.size()-1,inorder,0,inorder.size()-1); } TreeNode* buildTreeHelper(vector<int>&postorder, int pstart, int pend, vector<int>&inorder, int istart, int iend) { if(pstart > pend || istart > iend) return NULL; TreeNode* root = new TreeNode(postorder[pend]); int index = map[root->val]; int num = index - istart; root->left = buildTreeHelper(postorder,pstart, pstart + num - 1, inorder,istart,index - 1); root->right = buildTreeHelper(postorder,pstart + num, pend-1,inorder,index+1,iend); return root; }};
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
- 多线程之间共享的资源有哪些
- gb2312转utf-8
- Unity3d游戏开发框架-标志量-数学管理类-时间管理-Log日志管理
- 码农如何在其他码农面前一本正经的装逼?懂点硬件知识
- Codeforces 711A- Bus to Udayland
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 传输控制协议--摘录
- Lua学习笔记(三)
- OpenGLES编程思想
- Go-指针、传值与传引用、垃圾回收
- MFC学习(3)对话框
- 【POJ 2454】Jersey Politics(RPの神Rand)
- 程序员兵器谱,带评论
- sicp中stream的python实现