1、



Construct Binary Tree from Inorder and Postorder Traversal

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

Note:

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

代码:

class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
TreeNode* root = NULL;
int len1 = inorder.size();
int len2 = postorder.size();
if(len1<1 || len2<1 || len1!=len2){
return root;
}
return buildTreeCore(inorder,0,len1-1,postorder,0,len2-1);
}
TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&postorder, int startIndex2, int endIndex2){
if(endIndex1 < startIndex1 ){
return NULL;
}
TreeNode* root = new TreeNode(postorder[endIndex2]);
int index = 0;
for(int i=startIndex1; i<=endIndex1; ++i){
if(inorder[i] == postorder[endIndex2]){
index = i;
break;
}
}
int leftLen = index-startIndex1;
TreeNode* leftNode = NULL;
TreeNode* rightNode = NULL; leftNode = buildTreeCore(inorder,startIndex1,index-1,postorder,startIndex2,startIndex2+leftLen-1);
rightNode = buildTreeCore(inorder,index+1,endIndex1,postorder,startIndex2+leftLen,endIndex2-1); root->left = leftNode;
root->right = rightNode;
return root;
}
};

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

class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
TreeNode* root = NULL;
int len1 = inorder.size();
int len2 = preorder.size();
if(len1<1 || len2<1 || len1!=len2){
return root;
}
return buildTreeCore(inorder,0,len1-1,preorder,0,len2-1);
}
TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&preorder, int startIndex2, int endIndex2){
if(endIndex1 < startIndex1 ){
return NULL;
}
TreeNode* root = new TreeNode(preorder[startIndex2]);
int index = 0;
for(int i=startIndex1; i<=endIndex1; ++i){
if(inorder[i] == preorder[startIndex2]){
index = i;
break;
}
}
int leftLen = index-startIndex1;
TreeNode* leftNode = NULL;
TreeNode* rightNode = NULL; leftNode = buildTreeCore(inorder,startIndex1,index-1,preorder,startIndex2+1,startIndex2+leftLen);
rightNode = buildTreeCore(inorder,index+1,endIndex1,preorder,startIndex2+leftLen+1,endIndex2); root->left = leftNode;
root->right = rightNode;
return root;
}
};

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f的更多相关文章

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

  2. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  3. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  4. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

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

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

  9. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. React中的事件处理为什么要bind this?

    个人总结: 问: 请给我讲一下React中的事件处理为什么要bind this? 答: 好的,比如说我写了一个类组件,有个onClick属性 ,onClick={ this.fun },如果不bind ...

  2. tinymce原装插件源码分析(一)-hr

    tinymce简介 tinymce是一款能方便无限扩展的网页富文本编辑器. tinymce原装插件已经十分丰富,对于文本编辑(blog等文章)是绰绰有余,但是应对一些复杂的应用,比如在上面开发html ...

  3. JQ 添加节点和插入节点的方法总结

    转载来源:http://blog.csdn.net/ss1106404013/article/details/49274345 添加节点的jQuery方法: append().prepend().ap ...

  4. 处理Oracle 11g在用EXP导出时,空表不能导出

    一.问题原因:     11G中有个新特性,当表无数据时,不分配segment,以节省空间 想要给空表也分配segmant,有以下两个办法: 1.insert一行,再rollback就产生segmen ...

  5. Precision and recall From Wiki

    Precision.全部推断为正样本的数量里面,有多少是真正的正样本.就是精确率 Recall.所有的正样本里面,检測到了多少真正的正样本,又称查全率.即所有正样本查找到了多少的比率.

  6. 【C++探索之旅】第二部分第一课:面向对象初探,string的惊天内幕

    内容简单介绍 1.第二部分第一课:面向对象初探.string的惊天内幕 2.第二部分第二课预告:掀起了"类"的盖头来(一) 面向对象初探,string的惊天内幕 上一课<[C ...

  7. C++基础之全局变量

    C++的水比較深,之前我一直以为C++的全局变量会像其它语言一样,很easy仅仅要在头文件里,定义一个变量就可以,比方以下的test.h: #ifndef _TEST_H #define _TEST_ ...

  8. [BZOJ3670] [NOI2014] 动物园 解题报告 (KMP)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅, ...

  9. [JZOJ4274] [NOIP2015模拟10.28B组] 终章-剑之魂 解题报告(二进制)

    Description [背景介绍]古堡,暗鸦,斜阳,和深渊……等了三年,我独自一人,终于来到了这里……“终焉的试炼吗?就在这里吗?”我自言自语道.“终焉的试炼啊!就在这里啊!”我再一次自言自语道.“ ...

  10. Linux安装PHP和MySQL

    Linux上安装php运行环境稍微比Windows复杂,没有Windows那么方便的集成环境.技术在于折腾嘛 Linux 版本的可以参考之前发布的Linux安装PHP MongoDB扩展 安装环境 系 ...