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 ...
随机推荐
- C# 之 FTPserver中文件上传与下载(一)
近期接手这样一个文件上传到ftpserver的一个功能,接下来就给大家解析一下这一功能. 首先,今天我们要讲的是怎么创建一个FTPserver. 1.首先我们创建一个用户,当然不想创建 ...
- dbutil组件的常见用法
该工具包主要用来操作数据库,进行增删改查.将结果包装到对象或对象集合中. 在写web项目的时候,经常会涉及到数据库的操作.比如连接数据库获取连接对象.执行sql语句.获得结果.如果对每一个方法都写这么 ...
- SPD
一般是在内存条上的.1.SPD是SERIAL PRESENCE DETECT的缩写,中文意思是模组存在的串行检测.也即是通过上面讲的IIC串行接口的EEPROM对内存插槽中的模组存在的信息检查.这样的 ...
- UIScrollView 滚动视图—IOS开发
转自:http://blog.csdn.net/iukey/article/details/7319314 UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 CGRect ...
- 计算机图形学(一) 视频显示设备_1_CRT原理
第 1 章 图形系统概述 如今.计算机图形学的作用与应用已经得到了广泛承认.大量的图形硬件和软件系统已经应用 到了差点儿全部的领域.通用计算机甚至很多手持计算器也已经普遍具备 二维及三维 ...
- JQM事件详解
在前文<使用 jQuery Mobile 与 HTML5 开发 Web App —— jQuery Mobile 默认配置与事件基础>中,Kayo 对 jQuery Mobile 事件的基 ...
- Spring学习10-SpringMVC原理及核心组件1
一.SpringMVC原理 请求到来时,第一个接受这个请求的前端控制器叫DispatcherServlet(这个需要在web.xml中配置), 后端控制器叫Controller.负责处理请求U ...
- Nginx 使用中文URL,中文目录路径
Nginx 使用中文URL,中文目录路径 分类: linux2012-05-03 11:04 2672人阅读 评论(0) 收藏 举报 nginxurl服务器translationcentosserve ...
- 电影大全 API接口
http://www.apix.cn/services/show/29 http://www.apix.cn/services/show/112
- 每日英语:A Better Way To Treat Anxiety
Getting up the nerve to order in a coffee shop used to be difficult for 16-year-old Georgiann Steely ...