Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:什么桌面壁纸软件好 编辑:程序博客网 时间:2024/06/06 17:10
/** * Definition for binary tree * 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) { if(inorder.empty() || postorder.empty()) return NULL;return buildTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); }TreeNode *buildTree(const vector<int> &inorder,int inLeft,int inRight,const vector<int> &postorder,int postLeft,int postRight){if(inLeft>inRight || postLeft>postRight) return NULL;//根节点TreeNode *root=new TreeNode(postorder[postRight]);//计算左子树结点数int i=0;for(i=inLeft;i<=inRight;i++){if(inorder[i]==postorder[postRight]){break;}}const int leftSubTree=i-inLeft;root->left=buildTree(inorder,inLeft,i-1, postorder,postLeft,postLeft+leftSubTree-1);root->right=buildTree(inorder,i+1,inRight, postorder,postLeft+leftSubTree,postRight-1);return root;}};
0 0
- *(leetcode) Construct Binary Tree from Inorder and Postorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 110: Construct Binary Tree from Inorder and Postorder Traversal
- 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Inorder and Postorder Traversal
- 花样作死错误锦集。
- hdu 1809 SG函数
- vc 位图操作 BITMAPINFO 赋值
- Java求100-999之间所有的"水仙花数"
- 收藏下一个适合用来做宣传页背景的色调和CSS代码
- Construct Binary Tree from Inorder and Postorder Traversal
- 维吾尔族大学生:我没有伤害人 却有负罪感
- 白话Java应用打包
- hdu2602 Bone Collector (01背包)
- HDU 1754 线段树单点更新求最大值
- 基于中颖SH88F516驱动TM1640的LED数码管驱动程序
- vs2008快捷键
- TM1640操作源码--LED驱动IC
- java nio 之MappedByteBuffer