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 ...
随机推荐
- Yeslab 华为安全HCIE-第七门-Agile Controlle
课程目录: 华为安全HCIE-第七门-Agile Controller(12篇)\1_aglie_controller产品亮点讲解.avi 华为安全HCIE-第七门-Agile Controlle ...
- Python 爬取单个网页所需要加载的地址和CSS、JS文件地址
Python 爬取单个网页所需要加载的URL地址和CSS.JS文件地址 通过学习Python爬虫,知道根据正式表达式匹配查找到所需要的内容(标题.图片.文章等等).而我从测试的角度去使用Python爬 ...
- POJ——T 3067 Japan
http://poj.org/problem?id=3067 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29474 ...
- Android Studio升级到0.8.1后怎样设置字体大小?
升级到0.8.1后.打开设置字体大小页面.你会发现无论是Default还是Darcula,都不同意你改变字体的大小.事实上这个是由于这两个模式是Android Studio自带模式,所以不同意你修改, ...
- FFmpeg的HEVC解码器源码简单分析:解码器主干部分
===================================================== HEVC源码分析文章列表: [解码 -libavcodec HEVC 解码器] FFmpeg ...
- 小白算法之路-非确定性多项式(non-deterministic polynomial,缩写NP)
前端小白的算法之路 时隔多日终于解决了埋在心头的一道难题,霎时云开雾散,今天把一路而来碰到的疑惑和心得都记录下来,也算是开启了自己探索算法的大门. 问题背景 曾经有一个年少轻狂的职场小白,在前端圈 ...
- vue 引入第三方字体包
1.创建 style_css 文件夹,在当前文件夹下 新建 index.css 和 引入字体包. 2. 在build 目录下,设置 webpack.base.cong.js , 在module ...
- 关于自定义一个上传的file按钮
在input中html给我们一个 type file用来做文件上传的功能,比如 但是这样的样式,实在难看,在开发的时候看了layui和bootstrap的点击上传,都很不错. 前者的调用方式和js的 ...
- 【基础篇】DatePickerDialog日期控件的基本使用(一)
项目步骤: 1.首先在Main.xml布局文件中添加一个Button标签,用来点击显示日期控件,Main.xml内容如下: <RelativeLayout xmlns:android=" ...
- Codeforces Round #196 (Div. 2) 少部分题解
A:sort以后求差值最小 ]; int main() { int n,m; cin>>n>>m; ; i < m ; i++) cin>>a[i]; sor ...