Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
2024-09-02 11:55:52
总结:
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;
}
最新文章
- WC项目
- oracle的用户
- 基于jQuery打造的选项卡向上弹出jquery焦点图切换特效
- 如何创建Asp.net MVC ViewModel
- 在自己的网站上实现QQ授权登录
- UVA 644 Immediate Decodability (字符处理)
- DevOps之服务器
- c++代码的编译
- Memocache 详细的工作机制
- redis在实践中的一些常见问题以及优化思路
- JVM笔记——编译期的优化
- jsfl 常用方法
- MT【178】平移不变性
- web_submit_data详解
- 洛谷P3389 高斯消元 / 高斯消元+线性基学习笔记
- 【转】ArcGIS API for Silverlight/WPF 2.1学习笔记(二)
- 提高搜狗SR值和关键词排名
- linux环境变量设置 以及 source命令 Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程 Linux 设置环境变量
- leetcode-479-Largest Palindrome Product(找到两个乘数相乘得到的最大的回文数)
- python学习,day3:函数式编程,递归和高阶函数
热门文章
- 通讯录结构体方法的实现 和VS中存在的一些问题的分析
- 抛弃鼠标的神器——Vimium
- 【LeetCode】90. Subsets II (2 solutions)
- C++求两个数的最大值
- TypeError: can&#39;t convert console.log(...) to primitive type
- xdebug安装教程
- 0x01 译文:Windows桌面应用Win32开发简介
- Atitit.各种 数据类型 ( 树形结构,表形数据 ) 的结构与存储数据库 attilax 总结
- Atitit. 解决unterminated string literal 缺失引号
- Swift 3.1 的一些新特性