[LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:网络支付清算平台 编辑:程序博客网 时间:2024/06/06 05:02
Construct Binary Tree from Preorder and Inorder Traversal
问题描述:Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
分析:
1、先序遍历第一个元素是该树的根
2、中序遍历该元素将树分为左右两个子树,该元素设为i
3、中序0~i-1对应于先序的1~i元素,中序右子树i+1~in_end对应于先序i+1~pre_end
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
//递归,时间复杂度 O(n),空间复杂度O(logn)
class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { return build(begin(preorder),end(preorder),begin(inorder),end(inorder)); } template<typename InputIterator> TreeNode* build(InputIterator pre_first, InputIterator pre_last, InputIterator in_first, InputIterator in_last) { if(pre_first == pre_last) { return nullptr; } if(in_first == in_last) { return nullptr; } auto root = new TreeNode(*pre_first); auto inRootPos = find(in_first, in_last, *pre_first); auto leftSize = distance(in_first, inRootPos); root->left = build(next(pre_first),next(pre_first,leftSize+1),in_first,next(in_first,leftSize)); root->right = build(next(pre_first,leftSize+1),pre_last,next(inRootPos),in_last); return root; } };
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反射机制
- 大数处理
- 操作系统进程调度,优先级反转,调度策略
- NS3学习记录(二)---eclipse调试NS3配置
- OSX中php多进程安装pcntl
- [LeetCode]Construct Binary Tree from Preorder and Inorder Traversal
- NS3学习记录(三)---一些统计工具安装
- 实现提取南工oj的题号、标题、通过率
- 如何在Eclipse中配置python开发环境
- 【Axure教程】Axure中使用表格的技巧
- Shell编程入门(第二版)(上)
- android笔记之--向网络发送GET/POST请求参数
- 文件IO的基本操作(二)
- NS3学习记录(四)--加入新模型及聚合Aggregate