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. 堆(Heap)-c实现

    这个堆的实现采用数组存储的完全二叉树实现. 最近有点烦躁,先是跳槽到了一个外包公司,感觉2016有点坑,另外一件事就是老婆怀孕了,但是在家里没人照顾,很担心. 这个堆的实现就暂时不优化了,基本的插入, ...

  2. python 中i++、逻辑表达式

    参考链接:https://www.cnblogs.com/yupeng/p/3345946.html i++运算符 python中没有类似i++之类实现+1的运算符,但是有++i,+-i.之类的,他们 ...

  3. 【codeforces 589G】Hiring

    [题目链接]:http://codeforces.com/problemset/problem/589/G [题意] 有n个人; 每个人每天在开始工作之前,都需要di单位的准备时间,然后才能开始工作; ...

  4. poj3134 Power Calculus IDA*

    好端端的一道搜索题目,,,硬生生的被我弄成了乱搞题,,,枚举当前的maxd,深搜结果,然而想到的剪枝方法都没有太好的效果,,,最后用一个贪心乱搞弄出来了,,, 贪心:每次必用上一次做出来的数字与其他数 ...

  5. Java中的字符串常量池和JVM运行时数据区的相关概念

    什么是字符串常量池 JVM为了减少字符串对象的重复创建,其维护了一个特殊的内存,这段内存被成为字符串常量池或者字符串字面量池 工作原理 当代码中出现字面量形式创建字符串对象时,JVM首先会对这个字面量 ...

  6. EularProject 42:单词解码出来的三角形数

    Coded triangle numbers Problem 42 The nth term of the sequence of triangle numbers is given by, tn = ...

  7. [Javascript] Simplify Creating Immutable Data Trees With Immer

    Immer is a tiny library that makes it possible to work with immutable data in JavaScript in a much m ...

  8. MySQL的登录和退出(五)

    如何使用MySQL? 如何实现MySQL的登录/退出 如何修改MySQL的提示符 如何实现MySQL的常用命令 如何规范MySQL语句 如何操作数据库 1.MYSQL常用参数及功能 mysql -V ...

  9. angular.js高级程序设计书本开头配置环境出错,谁能给解答一下

    server.jsvar connect=require('connect');serveStatic=require('serve-static');var app=connect();app.us ...

  10. 离奇失踪的WM_HOTKEY消息--浅析WIN32消息队列

    故事的开端有些平淡,眼红于XXX小程序,认为写完该程序就有了和心仪的妹子多相处的机会,必须搞,必须酷,按钮不能有,界面得隐藏,这就想到了全局快捷键. 注册调用RegisterHotKey(m_hWnd ...