一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源:https://leetcode.com/problems/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 duplicates do not exist in the tree.

(二)解题

题目大意:根据二叉树的前序和中序遍历,构造出该二叉树

剑指offer上的老题了,前序遍历的第一个节点为根节点,在中序遍历中找到该节点,其左边为根节点的左子树,后边为根节点的右子树。依次递归下去即可以重构出该二叉树。

如:123和213,前序遍历找出根节点为1,在中序遍历213中找出1,则2为左子树,3为右子树。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    typedef vector<int>::iterator vi;
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.empty()||inorder.empty()) return (TreeNode*)NULL;
        vi preStart = preorder.begin();
        vi preEnd = preorder.end()-1;
        vi inStart = inorder.begin();
        vi inEnd = inorder.end()-1;
        return constructTree(preStart,preEnd,inStart,inEnd);
    }
    TreeNode* constructTree(vi preStart,vi preEnd,vi inStart,vi inEnd)
    {
        //表示该节点为NULL
        if(preStart>preEnd||inStart>inEnd) return NULL;
        //前序遍历的第一个节点为根节点
        TreeNode* root = new TreeNode(*preStart);
        //只有一个节点的时候直接返回
        if(preStart==preEnd||inStart==inEnd) return root;
        vi rootIn = inStart;
        while(rootIn!=inEnd){//在中序遍历中找出根节点
            if(*rootIn==*preStart) break;
            else ++rootIn;
        }
        root->left = constructTree(preStart+1,preStart+(rootIn-inStart),inStart,rootIn-1);//递归构造左子树
        root->right = constructTree(preStart+(rootIn-inStart)+1,preEnd,rootIn+1,inEnd);//递归构造右子树
        return root;
    }
};

【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

  2. (二叉树 递归) 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 ...

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

  4. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. Java for 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 ...

  7. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  8. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

  9. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

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

随机推荐

  1. salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型

    看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...

  2. SSH构造struts2项目

    第一在pom.xml导入相应的包 (网上有很多导入多个包的教程,我缩减到一个了) <project xmlns="http://maven.apache.org/POM/4.0.0&q ...

  3. hibernate实体对象的三种状态:自由状态,持久状态,游离状态.

    自由态与游离态的区别: 当一个持久化对象,脱离开Hibernate的缓存管理后,它就处于游离状态,游离对象和自由对象的最大区别在于,游离对象在数据库中可能还存在一条与它 对应的记录,只是现在这个游离对 ...

  4. svg和css实现波浪动效

    效果: 截图有点模糊~ 实现: <svg教程> //html <body> <svg class="wave-container" xmlns=&qu ...

  5. Chrome浏览器Postman插件安装使用

    最近调试后台接口一直在使用的工具,由于换了新的电脑重新安装了一下PostMan.随便记录一下如何安装使用这个插件. 闲言不要谈,直接上步骤: 1. 首先必须有chrome浏览器,这个相信大家肯定都安装 ...

  6. 监控undo空间和临时段的使用情况

    --1.监控undo空间情况 ),) free_space from dba_free_space where tablespace_name='UNDOTBS1' group by tablespa ...

  7. Docker常见仓库Nginx

    Nginx 基本信息 Nginx 是开源的高效的 Web 服务器实现,支持 HTTP.HTTPS.SMTP.POP3.IMAP 等协议. 该仓库提供了 Nginx 1.0 ~ 1.7 各个版本的镜像. ...

  8. GDAL C#中文路径,中文属性名称乱码问题

    昨天写的博客,将C#读取shp中文属性值乱码的问题应该可以解决,博客地址为:http://blog.csdn.net/liminlu0314/article/details/54096119,然后又测 ...

  9. ROS机器人程序设计(原书第2版)补充资料 kinetic

    Effective Robotics Programming with ROS Third Edition Find out everything you need to know to build ...

  10. 用Netty解析Redis网络协议

    用Netty解析Redis网络协议 根据Redis官方文档的介绍,学习了一下Redis网络通信协议.然后偶然在GitHub上发现了个用Netty实现的Redis服务器,很有趣,于是就动手实现了一下! ...