总结:

1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标

代码: 前序中序

#include <iostream>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<int> preorder, inorder;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
TreeNode * root = NULL;
if(preorder.size() == 0 || inorder.size() == 0)
return root; this->preorder = preorder;
this->inorder = inorder;
for(int i = 0; i < inorder.size(); i ++) {
if(inorder[i] == preorder[0]) {
root = new TreeNode(preorder[0]);
int len1 = i;
int len2 = inorder.size()-i-1;
root->left = buildParty(1,0, len1);
root->right = buildParty(len1+1, i+1, len2);
return root;
}
}
}
TreeNode *buildParty(const int &p, const int &i, const int &len) {
if(len <= 0)
return NULL;
for(int cursor = 0; cursor < len; cursor++) {
int pos = cursor+i; if(inorder[pos] == preorder[p]) {
TreeNode *root = new TreeNode(preorder[p]);
int len1 = cursor;
int len2 = len-cursor-1;
root->left = buildParty(p+1, i, len1);
root->right = buildParty(p+len1+1, pos+1, len2);
return root;
}
}
}
}; int main() {
TreeNode *node; int in1[10] = {1, 2, 3, 4, 5, 6};
int in2[10] = {3, 2, 4, 1, 5, 6}; Solution solution;
node = solution.buildTree(vector<int>(in1, in1+6), vector<int>(in2, in2+6));
return 0;
}

  

代码: 中序后序

#include <iostream>
#include <vector>
using namespace std; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
vector<int> inorder;
vector<int> postorder;
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
TreeNode *root = NULL;
if(!inorder.size())
return root; this->inorder = inorder;
this->postorder = postorder; for(int ci = 0; ci < inorder.size(); ci++) {
if(inorder[ci] == postorder[postorder.size()-1]) {
root = new TreeNode(inorder[ci]);
int len1 = ci;
int len2 = inorder.size()-ci-1;
root->left = buildParty(0, postorder.size()-len2-2, len1);
root->right = buildParty(ci+1, postorder.size()-2, len2);
return root;
} }
}
TreeNode *buildParty(const int &i, const int &j, const int &len) {
if(!len)
return NULL; for(int ci = 0; ci < len; ci ++) {
int pos = i+ci;
if(postorder[j] == inorder[pos]) {
TreeNode *root = new TreeNode(inorder[pos]);
int len1 = ci;
int len2 = len-ci-1;
root->left = buildParty(i, j-len2-1, len1);
root->right = buildParty(i+ci+1, j-1, len2);
return root;
}
}
}
}; int main() {
TreeNode *node; int in1[10] = {3, 2, 4, 1, 5, 6};
int in2[10] = {3, 4, 2, 6, 5, 1}; Solution solution;
node = solution.buildTree(vector<int>(in1, in1+6), vector<int>(in2, in2+6));
return 0;
}

  

Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

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

  2. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

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

  5. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  6. (二叉树 递归) 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 ...

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

  8. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

  10. Construct Binary Tree from Preorder and Inorder Traversal [LeetCode]

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. Linux命令-帮助命令:help

    which cd 查找cd命令的位置 which umask 查找umask命令的位置 Shell就是命令解释器,就是当你敲了一个命令,谁来把这个命令传递给内核,内核运行结束后返回一个结果,是谁把这个 ...

  2. Java Socket网络编程Server端详解

    Socket通信:分为客户端和服务端的socket代码. Java SDK提供一些相对简单的Api来完成.对于Java而言.这些Api存在与java.net 这个包里面.因此只要导入这个包就可以开始网 ...

  3. Android的学习之路(四)项目中清单文件的学习和android中经常使用的显示单位

    1.所谓的清单文件就是项目中的AndroidManifest.xml文件.这个文件但是有大用处的.比方:app的名字,图标.app支持的版本号app的包名等等.以下我就介绍下这个清单文件的各个參数的作 ...

  4. LIntcode---将二叉搜索树转成较大的树

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  5. 常用音频软件:Wavesufer

    作者:桂. 时间:2017-06-02  10:23:39 链接:http://www.cnblogs.com/xingshansi/p/6932408.html 前言 只列举两个自己用过的(wave ...

  6. unity, asset operations

    //----create asset //ref: http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset CmyScr ...

  7. 异步FIFO的FPGA实现

    本文大部分内容来自Clifford E. Cummings的<Simulation and Synthesis Techniques for Asynchronous FIFO Design&g ...

  8. 好工具 VHD

    通过powershell 互转 Convert-VHD –Path F:\debian.vhdx –DestinationPath F:\debian.vhd 举个栗子 附加参考 Convert-VH ...

  9. java ssm框架入门(三)正式项目的web.xml配置

    一个正规的上线的web.xml的配置. <?xml version="1.0" encoding="UTF-8"?> <web-app id= ...

  10. 连接数据库通过配置文件app.config

    ConfigurationManager类 public static class ConfigurationManager 命名空间: System.Configuration 程序集: Syste ...