106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:java web 跨域访问 编辑:程序博客网 时间:2024/06/04 19:19
106. Construct Binary Tree from Inorder and Postorder Traversal
思路:
recursion
code:
/** * 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: vector<int>::iterator getIndex(vector<int>::iterator inB, vector<int>::iterator inE, int target) { vector<int>::iterator index = inB; for (index; index != inE; index++) if (*index == target) break; return index; } TreeNode* inPost(vector<int>::iterator inB, vector<int>::iterator inE, vector<int>::iterator postB, vector<int>::iterator postE) { if (postE == postB) { return NULL; } else { TreeNode* root = new TreeNode(*(postE-1)); vector<int>::iterator inI = getIndex(inB, inE, root->val); root->left = inPost(inB, inI, postB, postB+(inI-inB)); root->right = inPost(inI+1, inE, postB+(inI-inB), postE-1); return root; } } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return inPost(inorder.begin(), inorder.end(), postorder.begin(), postorder.end()); }};
阅读全文
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
- 7. Cordova文件操作和IO
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- PhpStrom8.0.3配置Yaf
- Java 定时任务
- Larange插值
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 什么是N+1查询?
- React Js 微信禁止复制链接,分享,禁止隐藏右上角菜单
- java多态性
- 将二叉搜索树转换成链表
- C++ namespace 的一种用法
- Android SELinux avc dennied权限问题解决方法
- 砝码称重问题
- Vim好用的插件