当前位置:网站首页>Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
Leetcode 105. constructing binary trees from preorder and inorder traversal sequences
2022-07-22 18:49:00 【Great white sheep_ Aries】
Title Description
105. Construction of binary trees from traversal sequences of preorder and middle order
solution :
The specific process of constructing binary tree from preorder results and inorder results will not be introduced , Let's give a picture here about the determination of index
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);
The complete recursive construction code is as follows
/** * 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;
}
};
边栏推荐
猜你喜欢
【FatFs】基于STM32 SD卡移植FatFs文件系统
6-2-深度优先搜索 地下迷宫探索 (30分)
【QT源代码复用】模拟QCompleter的弹窗方式
1.雷点:mysql数据库转移到oracle,2.qt5.12.3 连接oracle 12c数据库
【 sdio】 résumé de l'analyse du Protocole sd2.0 (Ⅲ) - - Introduction aux commandes pertinentes de la carte SD
[STM32] STM32 SDIO SD card read / write test (II) -- SD_ Power on phase of init
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
【SDIO】SD2.0協議分析總結(三)-- SD卡相關命令介紹
Wechat official account web page authorization ----- redirect_ The URI domain name is inconsistent with the background configuration, and the error code is 10003
LeetCode 114. 二叉树展开为链表
随机推荐
总结20220214
Method of getting node binding data data index by applet
总结20220121
NRF24L01无线模块设置发射接受模式方法
JSON序列化对象时,如何返回有空值的带属性名称json字符串?
LeetCode: 620. 有趣的电影
07. Composite/aggregate Reuse Principle (carp)
NRF24L01 wireless module setting transmit receive mode method
PAT乙级1020月饼(注意测点)
总结20220213(floyd和dijkstra)
Leetcode 114. expand binary tree into linked list
LeetCode 653. 两数之和 IV - 输入 BST
ps: 如何调出辅助线
LeetCode 2028. 找出缺失的观测数据
QT的sprintf重写;qt下内容按界面的缩放而缩放(不改变字体大小)
Go 语言学习:Go 语言之旅(三)
03. simple responsibility principle
[audio] transplant wm8978 audio codec driver based on STM32 I2S
LeetCode:626. 换座位
02.依赖导致原则 - Dependence Inversion Principle