Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
总结:
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的更多相关文章
- [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 ...
- 【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 ...
- 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 ...
- 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 ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- (二叉树 递归) 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 ...
- [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 ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- 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 ...
随机推荐
- 【CentOS6.5】安装之DNS配置错误,yum install 软件报错:ERROR 6或者56错误提示”could not retrieve mirrorlist http://mirrorlist.centos.org ***”
刚安装完CentOS,使用yum命令安装一些常用的软件,使用如下命令:yum grouplist | more. 提示如下错误信息: Loaded plugins: fastestmirror Set ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [bug]未能从程序集“System.ServiceModel, Version=3.0.0.0问题解决
在Windows Server 2008中的IIS服务器中部署WCF服务程序时,通过浏览器访问报出如下错误: 未能从程序集“System.ServiceModel, Version=3.0.0.0, ...
- SQL Server 错误(待补充)
1.问题:在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接. 解决方法: 打开“ ...
- 摘:static,const,inline,define的意义
static 1) 产生背景 引出原因:函数内部定义的变量,在程序执行到它的定义处时,编译器为它在栈上分配空间,大家知道,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一个问题: 如果想 ...
- Redis配置和常用命令
redis.conf配置文件: 引用 #是否作为守护进程运行 daemonize yes #配置pid的存放路径及文件名,默认为当前路径下 pidfile redis.pid #Redis默认监听端口 ...
- Hadoop知识汇总
Hadoop的两大功能:海量数据存储和海量数据分析 Hadoop2的三大核心组件是:HDFS.MapperReducer和yarn 1.HDFS:分布式文件系统海量数据存储 2.MapperReduc ...
- JMeter学习笔记(四)
1. 断言 断言组件是通过获取服务器响应数据,然后根据断言规则去匹配这些响应数据:匹配到是正常现象,此时我们看不到任何提醒,如果匹配不到,即出现了异常情况,此时JMeter就会断定这个事务失败,那么我 ...
- python内置函数之vars()
vars([object]) 返回__dict__属性的值.当不传入参数时,和locals()等效.当函数接收一个参数时,参数可以是模块.类.类实例,或者定义了__dict__属性的对象. >& ...
- vue-router介绍
vue-router学习 转自:https://my.oschina.net/u/1416844/blog/849971 1. vue-router介绍 vue-router把react-router ...