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. 查看Linux 服务器是 32位还是64位的

    查看Linux 服务器是 32位还是64位的 getconf LONG_BIT 返回 64 代表就是 64位的: 返回 32 代表就是 32位的:

  2. NYIST 1019 G.亲戚来了

    G.亲戚来了 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 Bob 一家人要去下馆子,为什么呢?因为他姑姑的大爷的叔叔的孙子的表叔的婶婶的儿子来了,亲戚来了当然要下 ...

  3. vps上运行serv-u的问题

    为了给产品环境建个测试站,今天特意申请一个vps来做开发用,但运行了Serv-U的ServUDaemon.exe后始终提示: 响应: 530 User czhan cannot log in. 很无语 ...

  4. HTTP Analyzer(实时分析HTTP/HTTPS数据流)

    简述 HTTP Analyzer是一款实时分析HTTP/HTTPS数据流的工具.它可以实时捕捉HTTP/HTTPS协议数据,可以显示许多信息(包括:文件头.内容.Cookie.查询字符窜.提交的数据. ...

  5. (六)storm-kafka源代码走读之PartitionManager

    PartitionManager算是storm-kafka的核心类了,如今開始简单分析一下.还是先声明一下,metric部分这里不做分析. PartitionManager主要负责的是消息的发送.容错 ...

  6. C++写的UrlEncode和UrlDecode

    关于UrlEncode的实现(C++).网上有非常多不同的版本号.对须要编码的字符集的选取并不统一.那么究竟有没有标准呢?答案是有的.參见wiki 绝对不编码的,仅仅有字母.数字.短横线(-).下划线 ...

  7. Java transientkeyword使用小记

    1. transient的作用及用法 我们都知道一个对象仅仅要实现了Serilizable接口,这个对象就能够被序列化,java的这样的序列化模式为开发人员提供了非常多便利.我们能够不必关系详细序列化 ...

  8. What's the difference between Unicode and UTF-8?

    https://stackoverflow.com/questions/3951722/whats-the-difference-between-unicode-and-utf-8 If asked ...

  9. IIS访问站点,出现connection refused

    排查后,发现是因为使用了代理导致的. 需要设置 Don't use the proxy server for local addresses.

  10. FZOJ--2221-- RunningMan(水题)

    Problem 2221 RunningMan Accept: 4    Submit: 10 Time Limit: 1000 mSec    Memory Limit : 32768 KB Pro ...