题目链接

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的更多相关文章

  1. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. 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 ...

  3. 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 ...

  4. 【题解二连发】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 ...

  5. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  6. 【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 ...

  7. [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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. nginx 和 php超时设置

    nginx.conf ---  http节: keepalive_timeout 600; #客户端浏览器超时时间fastcgi_connect_timeout 600; #php-fpm连接超时时间 ...

  2. 爬虫--Scrapy-持久化存储操作2

    1.管道的高级操作 将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 需求:将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 1.需要在管道文件中编写对应平 ...

  3. maven创建项目,打包出可执行Jar

    官网参考 http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html 配置多种打包方式 这个例子也不错 https://bl ...

  4. 更改html代码后网页不更新

    写了一个非常简单的 html 页面,只有简单的跳转功能,但是在 Eclipse 下更改代码后用 chrome 浏览器打开时还是显示原来的网页.开始我以为是网页有错误或者有不规范的地方,因为我编写的是 ...

  5. $event Object angularjs

    You can pass the $event object as an argument when calling the function. The $event object contains ...

  6. 【377】only one element in a tuple

    Recently I am doing the assignment of COMP9021. It is too difficult and it is about the Knight and K ...

  7. Mac下如何安装WebStorm + 破解

    1.官网下载       下载地址   选择好系统版本以后,点击DOWNLOAD        2.安装 双击下载好的安装包.将WebStromt拖入application文件夹,然后在Launchp ...

  8. 关于HashMap多线程下环形链表的总结

    目录 1. 概述 2. 敲黑板的点 3. 为什么会出现循环链表的情况呢?(jdk1.7) 4. jdk1.8中改进了resize方法 5. HashMap的线程安全问题 6. 总结 1. 概述 本文主 ...

  9. Ldap-crack-test?

    ldap #!/bin/env python import sys import ldap ldapconn = ldap.initialize('ldap://domain.adserve.com' ...

  10. jira-6.0.1-x64下载地址

    http://downloads.atlassian.com/software/jira/downloads/atlassian-jira-6.0.1-x64.bin