106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:淘宝店怎么做百度推广 编辑:程序博客网 时间:2024/04/30 17:48
题目:
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
递归
/** * 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) { return buildTree_helper(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); } TreeNode* buildTree_helper(vector<int>& inorder,int start_in,int end_in, vector<int>& postorder,int start_post,int end_post){ if(start_in>end_in)return NULL; TreeNode* root = new TreeNode(postorder[end_post]); int i = find(inorder.begin()+start_in,inorder.begin()+end_in,root->val)-inorder.begin()-start_in; root->left = buildTree_helper(inorder,start_in,start_in+i-1, postorder,start_post,start_post+i-1); root->right = buildTree_helper(inorder,start_in+i+1,end_in, postorder,start_post+i,end_post-1); 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
- Android - LearnActivity
- MVC,MVP 和 MVVM 的图示
- JSP中页面向Action传递参数的几种方式
- UVa 11732 strcmp函数 trie树 左儿子右兄弟表示法
- JS中的call()和apply()方法
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- JAVA学习20160229
- 浅拷贝造成的内存 被重复释放
- iOS中监控软键盘显示或隐藏的可靠方法
- 在PHP语言中使用JSON
- 学习方法
- Android 5.1 Camera 架构学习(一)——Camera初始化
- 对MTK安卓平台LCM驱动的理解
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程