Given preorder and inorder traversal of a tree, construct the binary tree.

Note:

You may assume that duplicates do not exist in the tree.

1:注意特殊情况。2:递归的情况;3:递归结束情况;4:首先获得根节点,之后把两个数组分别分成两部分,递归分别得出左子树和右子树。

    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
if(preorder.size() == 0 || inorder.size() == 0 || preorder.size() != inorder.size())
{
return NULL;
} int size = (int)preorder.size(); return buildTreeCore(preorder, 0, inorder, 0, size);
} TreeNode* buildTreeCore(vector<int> &preorder, int preStart, vector<int> &inorder, int preIn, int length)
{
if(length == 1)
{
if(preorder[preStart] != inorder[preIn])
{
return NULL;
}
} int rootValue = preorder[preStart];
TreeNode *root = new TreeNode(rootValue); int i = 0; for(; i < length; i++)
{
if(inorder[preIn + i] == rootValue)
{
break;
}
} if(i == length)
{
return NULL;
} if(i > 0)
{
root->left = buildTreeCore(preorder, preStart + 1, inorder, preIn, i);
} if(i < length - 1)
{
root->right = buildTreeCore(preorder, preStart + i + 1, inorder, preIn + i + 1, length - 1 - i);
} return root;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

72_leetcode_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

    一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...

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

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

随机推荐

  1. hdu1573-X问题

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 中国剩余定理 #include<iostream> #include<cstdio> ...

  2. SQL ---指令实例语句

    1 1 create database+数据库名字 创建数据库 2 2 create table+表的名字 创建表 3 表中的操作: 4 3 insert into 表名 (列名1,列名2··)val ...

  3. enum可以做索引

    enum可以做索引 enum可以做索引, 配上虚函数,或者函数指针,可以实现上层的统一封装和快速索引. 点击(此处)折叠或打开 MoTbl.cpp #include <stdio.h> # ...

  4. Android 进行单元測试难在哪-part3

    原文链接 : HOW TO MAKE OUR ANDROID APPS UNIT TESTABLE (PT. 1) 原文作者 : Matthew Dupree 译文出自 : 开发技术前线 www.de ...

  5. SQL Server定时自动抓取耗时SQL并归档数据脚本分享

    原文:SQL Server定时自动抓取耗时SQL并归档数据脚本分享 SQL Server定时自动抓取耗时SQL并归档数据脚本分享 第一步建库 USE [master] GO CREATE DATABA ...

  6. OCA读书笔记(14) - 备份和恢复基本概念

    备份恢复概念 如何判断数据库的一致性 在mount状态下,oracle如何判断数据库的一致性 scn:system change number,它是数据库时钟 如何查询当前系统的scn: select ...

  7. 图像处理特征不变算子系列之Moravec算子(一)

    论文转载请注明出处:http://blog.csdn.net/kezunhai 1977年,Moravec提出了兴趣点(Points of Interests)的概念,并应用于解决Stanford C ...

  8. python 学习笔记 10 -- 正則表達式

    零.引言 在<Dive into Python>(深入python)中,第七章介绍正則表達式,开篇非常好的引出了正則表達式,以下借用一下:我们都知道python中字符串也有比較简单的方法, ...

  9. android 自己定义通知栏遇到的问题

    首先看报错信息: E/AndroidRuntime(12220): FATAL EXCEPTION: main E/AndroidRuntime(12220): Process: gn.com.and ...

  10. HTML学习笔记——各种居中对齐

    0.前言     水平居中基本方法--指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中. 样式例如以下: 1:margin:0px auto 2:margi ...