【二叉树经典问题】106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:大学 知乎 编辑:程序博客网 时间:2024/06/14 05:37
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) {} * }; */
解答:
题目要求通过中序(左、根、右)和后序(左、右、根)重建一棵二叉树,例如
/*例: 1 /\ 23 /\ 4 5后序:4 5 2 3 1中序:4 2 5 1 3*/构建的方法其实很简单:后序的最后一个元素是根,在中序中找到它,那么按照中序的特点,这个元素左边的就是左子树,右边的就是右子树,这样递归做下去就可以了。
代码:
class Solution {public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return dfs(inorder,postorder,0,inorder.size()-1,0,postorder.size()-1); } TreeNode* dfs(vector<int>& inorder,vector<int>& postorder,int is,int ie,int ps,int pe){ if(is>ie) return nullptr; TreeNode* root=new TreeNode(postorder[pe]); int pos; for(int i=is;i<=ie;i++){ if(inorder[i]==root->val){ pos=i; break; } } root->left=dfs(inorder,postorder,is,pos-1,ps,ps+pos-is-1);//because pe-ps=pos-1-is (the number of the rest elements should be equal) root->right=dfs(inorder,postorder,pos+1,ie,pe-ie+pos,pe-1);//pe-1-ps=ie-(pos+1) => ps=pe-ie+pos return root; }};说起来。。有想法和能把想法转换成代码这之间是有一定差距的。。。还有,看了别人的代码就“懂”了,自己写却不会,这是最悲伤的quq
这个问题还有非递归的解法,代码来自Discuss:
using namespace std;#define forLess(i, s, e)for(int i = (s); i < int(e); ++i)#define forto(i, s, e)for(int i = (s); i <= int(e); ++i)class Solution {public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { TreeNode* cur = NULL; int i = 0, j = 0; stack<TreeNode *> s; while(i < inorder.size() && j < postorder.size()){if(inorder[i] == postorder[j]){ TreeNode *t = new TreeNode(inorder[i]); t->left = cur; cur = t; i++; j++; } else{ if(!s.empty() && postorder[j] == s.top()->val){ TreeNode *t = s.top(); s.pop(); t->right = cur; cur = t; j++; } else{ TreeNode *t = new TreeNode(inorder[i]); t->left = cur; cur = NULL; s.push(t); i++; } } } while(!s.empty()){ TreeNode *t = s.top(); s.pop(); t->right = cur; cur = t; } return cur; }};
阅读全文
0 0
- 【二叉树经典问题】106. Construct Binary Tree from Inorder and Postorder Traversal
- 105\106. Construct Binary Tree from Inorder and Postorder\Preorder Traversal&重建二叉树
- 重建二叉树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
- [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
- 教你避免内存泄露
- HTTP请求头和响应头
- PHP编程效率的20个要点 (2015-09-25 14:37:49) 转载 ▼ 用单引号代替双引号来包含字符串,这样做会更快一些。因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,
- Unity Canvas UI line Renderer
- 前端对后台传过来是数组进行重新排序显示
- 【二叉树经典问题】106. Construct Binary Tree from Inorder and Postorder Traversal
- hdu6170(dp)
- spring cloud教程---Eureka
- pl sql
- angular2中ViewChild,索引值相关
- Spring Boot : CROS解决跨域问题(七)
- jzoj5336 【NOIP2017提高A组模拟8.24】提米树 (dfs序dp,奇异姿势dp)
- C语言 输入
- url带加号+,get时显示空格