[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:windows的用户名怎么改 编辑:程序博客网 时间:2024/04/27 13:30
Given preorder and inorder traversal of a tree, construct the binary tree.
/** * 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 *generate(vector<int> &preorder, vector<int> &inorder,int pl,int pr,int il,int ir){ TreeNode *root; if(pl > pr || il > ir) root = NULL; else{ root = new TreeNode(preorder[pl]); int i; for(i = il;i <= ir && inorder[i] != preorder[pl];i ++); root -> left = generate(preorder,inorder,pl + 1,pl + i - il,il,i - 1); root -> right = generate(preorder,inorder,pl + i - il + 1,pr,i + 1,ir); } return root; } TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { return generate(preorder,inorder,0,preorder.size() - 1,0,inorder.size() - 1); }};
0 0
- *(leetcode) Construct Binary Tree from Preorder and Inorder Traversal (tree)
- LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode][tree] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode Construct Binary Tree from Preorder and Inorder Traversal
- leetcode 109: Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
- [leetCode] Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- Android WiFi之SoftAP
- Eclipse绑定本地API
- 《数据结构》第六章 知识点结构导图
- 公司Android项目公共通用的函数和方法
- Window2008 R2(64位)使用codesmith连接Sqlite
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal
- CMD 各种命令符
- typedef 函数指针的用法(附例子)
- 基于微服务架构,改造企业核心系统之实践
- AFNetWorking使用
- nth_element ---- 比较优美的代码
- 12306回应大量信息泄露:经其他网站或渠道流出
- android应用中去掉标题栏的方法
- Peripheral Sensor实现方式