Construct Binary Tree from Inorder and Postorder Traversal
来源:互联网 发布:剪裁音乐软件 编辑:程序博客网 时间:2024/06/03 08:50
/**/** * 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 *createTree(vector<int> &inorder, int inBeg, int inEnd, vector<int> &postorder, int postBeg, int postEnd) { if (inBeg > inEnd) return NULL; int root = postorder[postEnd]; int index; for(int i = inBeg; i <= inEnd; i++) if (inorder[i] == root) { index = i; break; } int len = index - inBeg; TreeNode *left = createTree(inorder, inBeg, index - 1, postorder, postBeg, postBeg + len - 1); TreeNode *right = createTree(inorder, index + 1, inEnd, postorder, postBeg + len, postEnd - 1); TreeNode *node = new TreeNode(root); node->left = left; node->right = right; return node; } TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { // Start typing your C/C++ solution below // DO NOT write int main() function if (inorder.size() == 0) return NULL; TreeNode *head = createTree (inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1); return head; }};
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
- CareerCup 17中等难题 Q17.3 阶乘尾随零个数
- Construct Binary Tree from Preorder and Inorder Traversal
- Python 常用文件及目录操作
- 快速查看电脑端口占用情况命令行
- Universal-Image-Loader使用注意,Listview中加入SwipeLayout,删除某item的时候,会出现类似抖动的现象
- Construct Binary Tree from Inorder and Postorder Traversal
- sdgsdgsd 发送到水电费水电工水电工水电工第三个
- iOS的socket开发基础
- java Unicode 转 utf-8 汉字
- R初学系列笔记(1)
- 集合框架之Map
- Selenium2.0之WebDriver学习总结(2)
- IOS UIScrollView滚动到指定位置
- Linux常用的指令(二)