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. 9月mob(ShareSDK)活动预告,这个秋天非常热

    9月秋天来临,广州的天气依旧非常热,广州的活动氛围更热~ 先有GMGC B2B对接会在广州创新谷,再有上方网TFC全球移动游戏开发人员大会来袭,游戏圈的火越烧越旺,成都GMGDC全球移动游戏开发人员大 ...

  2. Java魔法堂:JVM的运行模式 (转)

    一.前言 JVM有Client和Server两种运行模式.不同的模式对应不同的应用场景,而JVM也会有相应的优化.本文将记录JVM模式的信息,以便日后查阅. 二.介绍 在$JAVA_HOME/jre/ ...

  3. (76) Clojure: Why would someone learn Clojure? - Quora

    (76) Clojure: Why would someone learn Clojure? - Quora ★ Why would someone learn Clojure?   Edit

  4. niu人

    金步国简历 金步国简历 基本资料 姓名 金步国 性别 男 年龄 30 籍贯 江苏 淮安 院校 同济大学 专业 土木工程 学历 本科肄业 工作经验 5年 期望地点 长江以南 期望薪水 18000/月 个 ...

  5. js动态添加Div

    利用JavaScript动态添加Div的方式有很多,在这次开发中有用到,就搜集了一下比较常用的. 一.在一个Div前添加Div <html> <body> <div id ...

  6. [PHP]利用MetaWeblog API实现XMLRPC功能

    [PHP]利用MetaWeblog API实现XMLRPC功能 | OWNSELF [PHP]利用MetaWeblog API实现XMLRPC功能 Windows Live Writer是一款小巧的写 ...

  7. JavaScript 基础优化(读书笔记)

    1.带有 src 属性的<script>元素不应该在其<script>和</script>标签之间再包含额外的 JavaScript 代码.如果包含了嵌入的代码,则 ...

  8. Andriod中绘(画)图----Canvas的使用具体解释

    转载请注明出处:http://blog.csdn.net/qinjuning     因为在网络上找到关于Canvas的使用都比較抽象,或许是我的逻辑思维不太好吧,总是感觉理解起来比較困难, 尤其是对 ...

  9. Java程序猿学习当中各个阶段的建议

    回答阿里社招面试如何准备,顺便谈谈对于Java程序猿学习当中各个阶段的建议   引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的 ...

  10. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...