当前位置:网站首页>Rebuild binary tree
Rebuild binary tree
2022-08-06 23:10:00 【In the island】
重建二叉树
输入一棵二叉树前序遍历和中序遍历的结果,请重建该二叉树.
注意 :
- 二叉树中每个节点的值都互不相同;
- 输入的前序遍历和中序遍历一定合法;
数据范围
树中节点数量范围 [ 0 , 100 ] [0,100] [0,100].
样例
给定:
前序遍历是:[3, 9, 20, 15, 7]
中序遍历是:[9, 3, 15, 20, 7]
返回:[3, 9, 20, null, null, 15, 7, null, null, null, null]
返回的二叉树如下所示:
3
/ \
9 20
/ \
15 7
算法
(递归) O ( n ) O(n) O(n)
递归建立整棵二叉树:先递归创建左右子树,然后创建根节点,并让指针指向两棵子树.
具体步骤如下:
- 先利用前序遍历找根节点:前序遍历的第一个数,就是根节点的值;
- Find the position of the current root node in an in-order traversal k k k ,则 k k k 左边是左子树的中序遍历``,右边是右子树的中序遍历;
- 假设左子树的中序遍历的长度是 l l l ,则在前序遍历中,根节点后面的 l l l 个数,是左子树的前序遍历,剩下的数是右子树的前序遍历;
- 有了左右子树的前序遍历和中序遍历,我们可以先递归创建出左右子树,然后再创建根节点;
时间复杂度分析
初始化时,用哈希表(unordered_map<int, int>)Record the position of each value in the in-order traversal,这样我们在递归到每个节点时,An operation that finds the location of the root node in an inorder traversal,只需要 O ( 1 ) O(1) O(1) 的时间.此时,创建每个节点需要的时间是 O ( 1 ) O(1) O(1) ,所以总时间复杂度是 O ( n ) O(n) O(n).
变量说明
pos:Record the position of each node in the in-order traversal sequence.pre: topic input data,Represents the preorder traversal sequence of the current binary tree.in: topic input data,Represents the inorder traversal sequence of the current binary tree.k: Indicates the position of the current root node in the given inorder traversal sequence(分割左右子树).pl:Indicates the starting position of the corresponding range in the preorder traversal sequence of the tree represented by the current root node.pr:Indicates the end position of the corresponding range in the preorder traversal sequence of the tree represented by the current root node.il:Indicates the starting position of the corresponding range in the in-order traversal sequence of the tree represented by the current root node.ir:Indicates the end position of the corresponding range in the in-order traversal sequence of the tree represented by the current root node.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
// Record the position of each point in the inorder traversal sequence
unordered_map<int, int> pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
int n = preorder.size();
for (int i = 0; i < n; i ++ )
pos[inorder[i]] = i;
return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
}
// DFS递归遍历二叉树
TreeNode* dfs(vector<int>& pre, vector<int>& in, int pl, int pr, int il, int ir) {
if (pl > pr) return NULL;
int k = pos[pre[pl]]; // The position of the current root node in the inorder traversal sequence
TreeNode* root = new TreeNode(pre[pl]);
root->left = dfs(pre, in, pl + 1, pl + k - il, il, k - 1);
root->right = dfs(pre, in, pl + k + 1 -il, pr, k + 1, ir);
return root;
}
};
边栏推荐
- OSPF综合实验
- 推荐一款管理系统专用低代码工具,一天开发一个系统不是梦!
- mysql5.5最简安装教程
- 【C语言】用最基础的C语言知识写一个简单的多子棋小游戏(源代码在文章末尾)
- bugku 这是一个单纯的图片
- 头脑风暴:打家劫舍
- Which securities company has low commission for account opening and accurate information? Is it safe to open a stock account with a mobile phone?
- PAT乙级-B1027 打印沙漏(20)
- bugku easy_nbt
- GBRank:一种基于回归的排序方法
猜你喜欢
随机推荐
PAT serie b - B1025 inversion list (25)
navicat连接oracle
Compose 进阶挑战来啦!直播预告 | 8 月 7 日晚 19:30 与 GDE 导师面对面
B. Suffix Operations
12条MySQL优化技巧,提速不止十倍!
Those MP3s who are reluctant to delete--modify the ID3tag of mp3 in batches
Julia两天极速入门学习笔记
IE彻底退出历史舞台 盘点那些年微软砍掉的产品
B. M-arrays
B. Jumps
HCIP笔记(十四)
HCIP笔记(十一)
cxf反向根据.net wsdl内容生成服务器端代码
HCIP笔记(十二)
micronet ICCV2021
Zoomlt 的使用方法
k8s部署redis集群(6节点,3主3从集群模式)
golang使用josn.Unmarshal报错:unexpected end of JSON input
深夜回家坐电梯选择站在哪个位置相对会更安全? 蚂蚁新村8月6日答案
猿人学-第一题









