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 ...
随机推荐
- PHP 相关配置
1. php-fpm的pool 编辑"php-fpm"配置文件"php-fpm.con" vim /usr/local/php/etc/php-fpm.conf ...
- JS关键字 import
今天开发时使用import作为方法名,报错 后查明报错原因:import是js中的关键字,在取方法名时不能取import
- 【 Codeforces Round #425 (Div. 2) D】Misha, Grisha and Underground
[Link]:http://codeforces.com/contest/832/problem/D [Description] 给你一棵树; 然后给你3个点 让你把这3个点和点s,t,f对应; 然后 ...
- python创建多层目录的方式
将 os.mkdir 改成 os.makedirs(opDir) 哈.
- 上下文切换查看 & sar
怀疑CPU存在瓶颈,可用sar -u 和sar -q来看,怀疑I/O存在瓶颈,可用sar -b.sar -u和 sar-d来看 sar –W 查看页面交换发生状况 [root@localhost ~] ...
- Hello World (记事本+命令行)
读完这篇博客.你将对下面几点更有心得: - Java 中的当前路径.类路径等概念 - javac.java 命令的综合使用 - jar 包的创建及引用 创建文件夹准备測试源代码 打包外部依赖 Jar ...
- 【php学习笔记】ticks篇
1. 什么是ticks 我们来看一下手冊上面对ticks的解释: A tick is an event that occurs for every N low-level statements exe ...
- Activity 之间 传递 List 封装的对象或者对象
项目中遇到 从也个页面向还有一个页面跳转传递一个List 封装的对象 .按网上查的资料 须要把 对象 实现 Serializable接口. 写了一下.可是跳转直接崩溃.一直看错误之日找不到原因后来自习 ...
- 如日中天的Uber到底是用什么开发语言做到的?
Uber将正在蓬勃发展的Go和Node.js这两个语言很好的融合到其系统上面来.Uber的站点可靠性project师Tom Croucher在近期于波兰举行的Node.js互动大会上详尽的对该公司所用 ...
- (转)OpenCV 基本知识框架
以下是对<学习OpenCV>一书知识框架的简单梳理 转自:http://blog.chinaunix.net/uid-8402201-id-2899695.html 一.基础操作 ...