当前位置:网站首页>LeetCode 105. 从前序与中序遍历序列构造二叉树
LeetCode 105. 从前序与中序遍历序列构造二叉树
2022-07-22 09:17:00 【大白羊_Aries】
题目描述
解法:
具体从前序结果和中序结果构造二叉树的流程就不再介绍,我们这里给出一幅图说一下关于索引的确定
int leftSize = index - inStart;
root->left = build(preorder, preStart + 1, preStart + leftSize,
inorder, inStart, index - 1);
root->right = build(preorder, preStart + leftSize + 1, preEnd,
inorder, index + 1, inEnd);
完整的递归构造代码如下所示
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return helper(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}
TreeNode* helper(vector<int>& preorder, int prestart, int preend, vector<int>& inorder, int instart, int inend){
if (prestart > preend) return nullptr;
int rootval = preorder[prestart], indx = 0;
for (int i = instart; i <= inend; i++)
if (inorder[i] == rootval) indx = i;
int leftsize = indx - instart;
TreeNode* root = new TreeNode(rootval);
root->left = helper(preorder, prestart + 1, prestart + leftsize, inorder, instart, indx - 1);
root->right = helper(preorder, prestart + leftsize + 1, preend, inorder, indx + 1, inend);
return root;
}
};
边栏推荐
- Add the GD Library under centos7.5, and then the MySQL expansion library. There is no problem without the configuration of MySQL expansion. There is no MySQL expansion in phpinfo
- 牛人专栏-博客汇总
- 04. interface aggregation principle
- Codeforces Round #806 (Div. 4)(7/7)
- 01.开闭原则 Open-Closed Principle
- [tools] how to set the comparison tool for TortoiseSVN to beyond compare 4
- Why does chrome video get stuck badly (by quqi99)
- Toss Phoenix system (by quqi99)
- LeetCode: 596. 超过5名学生的课
- 微信公众号网页授权----redirect_uri域名与后台配置不一致,错误码10003 错误
猜你喜欢
24 SaaS thoughts
01. Open closed principle
[Nordic] nrf52810 OTA upgrade (III) – DFU theoretical analysis
TCP and UDP, three handshakes and four waves
[audio] analysis and summary of PCM audio data transmitted by I2S (I)
[Nordic] nrf52810 OTA upgrade (II) – how to use DFU
[FatFs] FAT32 file system protocol summary (Theory + Practice)
Wechat official account web page authorization ----- redirect_ The URI domain name is inconsistent with the background configuration, and the error code is 10003
【SDIO】SD2.0协议分析总结(二)-- SD卡识别&数据传输过程
C language simple TCP server program
随机推荐
【FatFs】基于STM32 SD卡移植FatFs文件系统
[SDIO] SDIO, SD card, FatFs file system related article index
wampserver搭建到腾讯云服务,localhost能访问到apache,域名也能访问到自带的iis服务器,但域名访问不了
24 SaaS thoughts
LeetCode: 620. 有趣的电影
Summary 20220208 (KMP)
App移动端测试【6】应用程序(apk)包管理与activity
Codeforces Round #799 (Div. 4)(8/8)
【Audio】基于STM32 I2S移植WM8978 Audio Codec驱动
小程序获取节点绑定数据data-index的方法
【Audio】I2S传输PCM音频数据分析总结(一)
this指向问题
【Nordic】nRF52810 OTA升级(三)–DFU理论分析
How can ECSHOP run super slow locally?
Unity: 快速定位摄像机Camera
PTA 6-11 求自定类型元素序列的中位数 (25 分)
微信小程序本地访问本地TP5的路由接口正常,为何在手机上扫码预览获取不到数据?
Interview experience of Android Internet manufacturers
【SDIO】SD2.0协议分析总结(一)-- SD卡基本概率介绍
03.单一职责原则(Simple Responsibility Pinciple)