106. Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:云考勤软件 编辑:程序博客网 时间:2024/05/22 03:35
Given inorder and postorder traversal of a tree, construct the binary tree.
class Solution {public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { return dfs(inorder,postorder,0,postorder.size()-1,postorder.size()); } TreeNode* dfs(vector<int>& inorder, vector<int>& postorder,int inpos,int postpos,int length) { if(length==0) return NULL; TreeNode* root=new TreeNode(postorder[postpos]); int i; for(i=inpos;i<inpos+length;i++) { if(inorder[i]==postorder[postpos]) break; } int leftlength=i-inpos; int rightlength=length-leftlength-1; root->left=dfs(inorder,postorder,inpos,postpos-rightlength-1,leftlength); root->right=dfs(inorder,postorder,inpos+leftlength+1,postpos-1,rightlength); 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
- 今天再给大家带点html5前端开发的干货模板“text/tpl”怎么用 script template怎么用
- OpenCV中基于Haar特征和级联分类器的人脸检测
- UINavigationBar
- Android命名规范
- java标识符&&关键字
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- Uva 11624 - Fire!
- handler.post 为什么要将thread对象post到handler中执行呢?
- eclipse中格式化代码快捷键Ctrl+Shift+F失效
- Android 面试--小知识点
- unity3d 更换项目字体
- 正确制作一个iframe,认识iframe
- SPEC MPI 安装运行
- java ssh文件上传精确计算每个上传文件大小并显示进度