LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目
Construct Binary Tree from Preorder and Inorder Traversal
Total Accepted: 36475 Total Submissions: 138308My
Submissions
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
二.解题技巧
做法都是先依据先序遍历的概念,找到先序遍历的第一个值,即为根节点的值。然后依据根节点将中序遍历的结果分成左子树和右子树。然后就能够递归的实现了。
三.实现代码
- #include <iostream>
- #include <algorithm>
- #include <vector>
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- using std::vector;
- using std::find;
- struct TreeNode
- {
- int val;
- TreeNode *left;
- TreeNode *right;
- TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- };
- class Solution
- {
- private:
- TreeNode* buildTree(vector<int>::iterator PreBegin, vector<int>::iterator PreEnd,
- vector<int>::iterator InBegin, vector<int>::iterator InEnd)
- {
- if (PreBegin == PreEnd)
- {
- return NULL;
- }
- int HeadValue = *PreBegin;
- TreeNode *HeadNode = new TreeNode(HeadValue);
- vector<int>::iterator LeftEnd = find(InBegin, InEnd, HeadValue);
- if (LeftEnd != InEnd)
- {
- HeadNode->left = buildTree(PreBegin + 1, PreBegin + (LeftEnd - InBegin) + 1,
- InBegin, LeftEnd);
- }
- HeadNode->right = buildTree(PreBegin + (LeftEnd - InBegin) + 1, PreEnd,
- LeftEnd + 1, InEnd);
- return HeadNode;
- }
- public:
- TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
- {
- if (preorder.empty())
- {
- return NULL;
- }
- return buildTree(preorder.begin(), preorder.end(), inorder.begin(),
- inorder.end());
- }
- };
四.体会


LeetCode_Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] 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 ...
- [LeetCode] 105. 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 ...
- Leetcode 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 ...
随机推荐
- IT人都欠自已一个Lable Page
平时, 你是不是喜欢将一些工作或者技术的网站都会Mark一下.慢慢,自已收藏的标签越来越多.收藏后去查找的比较麻烦,而且不是很方便. 下面的文章是介绍我自已是怎么实现一个简单的Lable Page. ...
- WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了
原文:WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4 ...
- [Python] for.. not in.. Remove Deduplication
Write a function, remove_duplicates that takes a list as its argument and returns a new list contain ...
- 对string的一些扩展函数
对string作了一些扩展,包含string转化为int.string转化为double.string转化为bool.打印系统当前时间.但没有解决数据溢出的问题,请大神帮忙解决! //头文件 /*pa ...
- modSecurity规则学习(一)——配置文件
环境:modSecurity3.0,nignx1.13.8 modSecurity配置文件 1.nginx.conf server { listen ; modsecurity on; //启动mod ...
- Linux系统存储交换机日志
Linux系统存储交换机日志 日志记录是为系统设备在运行过程中报告其运行情况而设的, 为了保证系统正常运行, 解决每一天可能遇到的各种各样的问题, 网络管理员必须认真地读取日志记录.目前公司系 ...
- js实现 导航移入移出效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- AIX 5.3下创建逻辑卷、添加文件系统并挂载
首先创建逻辑卷smit lv ,这里没多大问题就不细述了. 输入要创建的逻辑卷名.所属卷组.分配多少个LP.创建在哪块磁盘上等,另外还可以设置镜像,默认是只有一份镜像的,即不做mirror. 到此LV ...
- canvas:画布
画布有默认宽度,如果要自己设置宽带要写在属性上 列: <canvas id = "myCanvas" width = "600" height = &qu ...
- ZJOI2005沼泽鳄鱼
矩阵优化dp ** 注意:矩阵乘法没有交换律 ** 思路:类比P2151hh去散步 代码特点在一维的答案矩阵 1.矩阵优化两点间方案数不必赘述 2.注意2,3,4可以办到以他们的lcm为周期,正是因为 ...