题目链接

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. jq动画分析

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. mysql 字符串数字转换

    1 方法一:SELECT CAST('123' AS SIGNED); 2 方法二:SELECT CONVERT('123',SIGNED); 3 方法三:select '123'+1

  3. java后端实习生面试题目

    1.编程题:java从10000到99999找到AABB类型 public class Test1 { public static void main(String[] args) { String ...

  4. Eclipse SVN文件冲突及不能直接提交情况

    下图为Eclipse SVN使用过程中存在文件冲突的情形. 以下是三种冲突情形及相应解决办法: 1.简单的文件版本冲突 情形:A改变了文件的头部,B改变了文件的尾部,如果两者改动互不影响,SVN可以智 ...

  5. ETC2 区别于ETC的重要点

    ETC2 主要是对于NPOT却是4的倍数的贴图有较大压缩,比如一个1920X1080RGB的Loading图,ETC4压缩下不管用,大小5.9M,ETC2下压缩为1M

  6. C++17尝鲜:编译期 if 语句

    Constexpr If(编译期 if 语句) 以 if constexpr 打头的 if 语句被称为 Constexpr If. Constexpr If 是C++17所引入的新的语法特性.它为C+ ...

  7. 【383】defaultdict 相关用法

    可以定义一个字典,可以添加默认值,int 为 0,list 为 [],set 为 {} int:默认值为 0 from collections import defaultdict int_dict ...

  8. python小实例一:简单爬虫

    本文所谓的爬虫就是通过本地远程访问url,然后将url的读成源代码形式,然后对源代码进行解析,获取自己需要的数据,相当于简单数据挖掘.本文实现的是将一个网页的图片爬出保存到本地的过程,例子很简单,用的 ...

  9. Zabbix监控系统配置

    1.Zabbix是一个基于WEB界面的提供分布式系统监控的企业级的开源解决方案 Zabbix能监视各种网络参数,保证服务器系统的安全稳定的运行,并提供灵活的通知机制以让SA快速定位并解决存在的各种问题 ...

  10. reids遇到问题

    今天重启爬虫服务器在连接redis数据库时突然报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not ...