[LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal
题目链接
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
题意
由二叉树的先序遍历和中序遍历建树
思路
理解建树过程;使用递归,递归关键:清楚递归过程,明确函数参数、返回值,写终止条件。
此外,注意空树的特判。
其他点
1 熟悉find()、assign()的使用。
2 使用
struct TreeNode *pNode =new TreeNode(*preRootIter);
而不是
struct TreeNode node=TreeNode(*preRootIter);
struct TreeNode* pNode=node;
否则造成assign的时候树中某些节点值被改变。
todo
上述问题原因待查。
应该最后要遍历树释放内存吧。
代码
#include <vector>
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(!preorder.size()){
return NULL;
}
else{
struct TreeNode* root=buildT(preorder,inorder);
return root;
}
}
private:
TreeNode* buildT(vector<int> preorder,vector<int> inorder){
vector<int>::iterator preRootIter=preorder.begin();
vector<int>::iterator midRootIter=find(inorder.begin(), inorder.end(), *preRootIter);
//create a new node
struct TreeNode *pNode =new TreeNode(*preRootIter);
// struct TreeNode node=TreeNode(*preRootIter);
// struct TreeNode* pNode=node;
//left child tree
vector<int>::size_type leftLen=midRootIter-inorder.begin();
vector<int>::iterator lPreBeg=preRootIter+1;
vector<int>::iterator lPreEnd=lPreBeg+leftLen;
if(!leftLen){
pNode->left=NULL;
}
else{
vector<int> preorderLeft;
preorderLeft.assign(lPreBeg, lPreEnd);
vector<int> inorderLeft;
inorderLeft.assign(inorder.begin(), midRootIter);
pNode->left=buildT(preorderLeft, inorderLeft);
}
//right child tree
vector<int>::size_type rightLen=inorder.end()-(midRootIter+1);
if(!rightLen){
pNode->right=NULL;
}
else{
vector<int> preorderRight;
vector<int>::iterator rPreBeg=lPreEnd;
vector<int>::iterator rPreEnd=rPreBeg+rightLen;
preorderRight.assign(rPreBeg, rPreEnd);
vector<int> inorderRight;
inorderRight.assign(midRootIter+1, inorder.end());
pNode->right=buildT(preorderRight, inorderRight);
}
return pNode;
}
};
int main(){
Solution solution;
vector<int> preorder={3,9,20,15,7};
vector<int> inorder={9,3,15,20,7};
struct TreeNode* tree=solution.buildTree(preorder,inorder);
return 0;
}
[LeetCode_105]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 OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
随机推荐
- Delphi 修改本地日期和时间
procedure TForm1.ModifySysdate(D: Double); var systemtime:Tsystemtime; DateTime:TDateTime; begin Set ...
- Delphi访问网页中的下拉菜单
Delphi通过TWebBrowser浏览网页,然后访问该网页中的下拉菜单: uses MsHtml;procedure TForm1.Button1Click(Sender: TObject);va ...
- es6 初级之let
1.在es6 中,定义变量使用 let. 1.1 var定义变量: <!DOCTYPE html> <html lang="en"> <head> ...
- sql中case when语句的使用
case when语句有两种格式:简单case函数和搜索case函数. --简单Case函数CASE sexWHEN '1' THEN '男'WHEN '2' THEN '女'ELSE '其他' EN ...
- Linux命令:zip
语法: zip [选项] zip文件 源文件s 选项 全称 含义 举例 -r recursive 递归压缩子目录里的文件(包括子目录里的子目录) zip -r target.z ...
- ReactiveX 学习笔记(19)使用 RxSwift + RxCocoa 进行 GUI 编程
课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...
- Android RxJava 2 的用法 just 、from、map、subscribe、flatmap、Flowable、Function、Consumer ...【转】
先简单说说RxJava的用途与价值 原文出处:Android RxJava 2 的用法 用途: 异步 (也就是开线程跳转) 价值: 面对复杂的逻辑,它依然 简洁 ,代码 易读 RxJava2 与 Rx ...
- Android文档 学习目录
Building Your First App After you've installed the Android SDK, start with this class to learn the b ...
- 吴裕雄 27-MySQL 元数据
你可能想知道MySQL以下三种信息:查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数.数据库和数据表的信息: 包含了数据库及数据表的结构信息.MySQL服务器信息: 包含 ...
- Centos7.x Docker桥接网络
基于Centos7.x构建Docker桥接网络, 配置bridge桥接网络可以直接设置网卡配置文件: 自定义桥接网络设置如下: 关掉docker0 ifconfig docker0 down 删除do ...