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 ...
随机推荐
- ubuntu建立软ap共享无线网络
建立ad-hoc模式共享网络 viewtopic.php?f=116&t=387194 有些android手机可能不支持ad-hoc模式,要第三方rom才行. 首先安装这些工具 代码: apt ...
- Linux命令-文件搜索命令:locate
实际上是在分区表上搜索文件,比find命令速度更快 locate inittab 查找名称包含inittab的所有信息(快速搜索,实际上它是搜索linux资料库,区别于find在某一个磁盘分区或者某一 ...
- DevExpress实现根据行,列索引来获取RepositoryItem的方法
/// <summary> /// 根据行,列索引来获取RepositoryItem /// </summary> /// <param name="view& ...
- ctags高级用法
1.ctags -R 有个问题,成员变量没有包含在里面.所以自动完成对象的成员时没有提示.解决办法:$ctags -R --fields=+iaS --extra=+q *–fields=[+|-]f ...
- sim900GPRS模块ppp拨号上网
--------------------------------------------- 主机操作系统:Centos 6.5 交叉编译器环境:arm-linux-gcc-4.5.4 开发板平台: F ...
- 让低版本IE也能正常运行HTML5+CSS3网站的3种解决方案
现在我们可以选择浏览器非常多,所以浏览器的环境也是种类繁多,同一个浏览器也是包含各种不同的版本,不同的版本之间的渲染方法也存在差异,,它们支持的 HTML5.CSS3 特性恐怕也不尽相同.这种情况于是 ...
- vue2.0 实现click点击当前li,并动态添加class(这种方法不太喜欢)
1,文件内容 ---- 使用v-for遍历数据 ---- @click="selectSort(item)"添加点击事件,并把每个obj=item传入 ---- v-show=&q ...
- 从零开始,跟我一起做jblog项目(二)Maven
从零开始,跟我一起做jblog项目(一)引言 从零开始,跟我一起做jblog项目(二)Maven maven是一个项目管理工具,尤其适用于JAVA世界 在jblog的开发前期,还没有系统使用过mave ...
- SAP ECC6安装系列五:安装后 License 的处理
原作者博客 http://www.cnblogs.com/Michael_z/ ======================================== 我发现我确实比较懒,先和各位说声抱歉了 ...
- linux 技巧:使用 screen 管理你的远程会话(短时间内同时开启多个会话)
screen -S zyj 开启一个会话窗口 会进入这个页面 然后按 Ctrl+a,再按一下d 退出 然后输入 screen -r -d zyj 会从新进入这个页面 如果你的工作完成就直接输入 关 ...