当前位置:网站首页>Two traversal sequences are known to construct binary trees
Two traversal sequences are known to construct binary trees
2022-07-02 15:39:00 【-Xiaoxiaobai-】
The traversal order of binary tree is : Access the root node first , The preorder then traverses the left subtree , Traverse the right subtree again in the preceding order .
The order of traversal is : The middle order traverses the left subtree of the root node , Then access the root node , Finally, the middle order traverses the right subtree .
The order of subsequent traversal is : Then traverse the left subtree of the root node , Then the post order traverses the right subtree , Finally, the root node is accessed .
One , Known before , Middle order traversal sequence to construct binary tree

【 Main idea 】
1. Traverse through the preamble , Know the root
2. Traverse through the middle order , Know the length of the left subtree .
3. Only the unknown of the right subtree is left , Just use recursion to build
// Auxiliary function
struct TreeNode* build(int* preorder, int l1, int r1, int* inorder, int l2, int r2){
if (l1 > r1) return NULL;
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = preorder[l1]; // Preorder traversal tells where the root is
int mid = l2;
while (inorder[mid] != root->val) mid++;
int leftSize = mid - l2; // The middle order traversal tells the length of the left subtree ( Just before the root )
// Recursively build
root->left = build(preorder, l1 + 1, l1 + leftSize, inorder, l2, mid - 1);
root->right = build(preorder, l1 + leftSize + 1, r1, inorder, mid + 1, r2);
return root;
}
struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize){
return build(preorder, 0, preorderSize - 1, inorder, 0, inorderSize - 1) ;
} Two , Known , After traversing the binary sequence tree
【 Main idea 】
1. The key is to figure out how the postorder traversal represents the left subtree 、 Right subtree 、 root
· The former sequence traversal : root / The left subtree / Right subtree -> Root in the head
· In the sequence traversal : The left subtree / root / Right subtree -> The length of the left subtree
· After the sequence traversal : The left subtree / Right subtree / root -> Root in tail
The relationship :
Determine the length of the left subtree by traversing the middle order : leftSize = left_in - mid
Before the order preorder Middle preface inorder In the following order postorder
The left subtree (left_pr + 1, left_pr + leftSize) || (left_in, mid - 1) || (left_po, left_po + leftSize - 1)
Right subtree (left_pr + leftSize + 1, right_pr) || (mid + 1, right_in) || (po + leftSize, right_po - 1)
*mid Because it was born in the middle order traversal , It can only be used in middle order traversal
* Why should the left subtree in post order traversal - 1? Because this is from “0” Array of starting numbers , Its subscript is to “ - 1”
* It can be inferred that the left subtree in preorder traversal does not need - 1. Because the head of preorder traversal is root , To make into “ from 1 Start ” Array of
// Auxiliary function
struct TreeNode* build(int* inorder, int left_in, int right_in, int* postorder, int left_po, int right_po) {
if (left_in > right_in) return NULL;
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = postorder[right_po];
int mid = left_in;
while (inorder[mid] != root->val) mid++;
int leftSize = mid - left_in;
root->left = build(inorder, left_in, mid - 1, postorder, left_po, left_po + leftSize - 1);
root->right = build(inorder, mid + 1, right_in, postorder, left_po + leftSize, right_po - 1);
return root;
}
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize){
return build(inorder, 0, inorderSize - 1, postorder, 0, postorderSize - 1);
}3、 ... and , Known before , After traversing the binary sequence tree

【 Main idea 】
1. First build the root , Then calculate the length of the left subtree , Build the left subtree , Finally, the right subtree . The whole process can be realized by recursion
2. The final root is easy to find , It is the first of the preorder traversal .
3. How to calculate the length of the left subtree ? According to the definition of preorder traversal and postorder traversal , I.e. preamble : root -> Left -> Right , In the following order : Left -> Right -> root , set up mid The pointer moves successively on the subsequent traversal , wait until mid The corresponding value is equal to the value of the root on the preorder traversal , At this moment mid Subtract the subscript at the beginning of traversal and add 1, You get the length of the left subtree
4. The length of the left subtree is obtained , Besides the root , The rest are right subtrees
【 Attention to detail 】
1. Pay attention to special situations , For example, the preorder traversal has only one node
2. use C When the language establishes a node , Remember to initialize . For example, a root spot , Remember root->left = NULL, root->right = NULL
// Auxiliary function
struct TreeNode* build(int* preorder, int left_pr, int right_pr, int* postorder, int left_po, int right_po) {
if (left_pr > right_pr) return NULL; Special circumstances that cannot be saved ①
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode)); //C Language establishment node method
root->val = preorder[left_pr];
root->left = NULL, root->right = NULL; // initialization root node , Omitting this step will report an error
if (left_pr == right_pr) return root; // Special circumstances that cannot be saved ②
int mid = left_po;
while (mid < right_po && postorder[mid] != preorder[left_pr + 1]) mid++;
int leftSize = mid - left_po + 1; // Get the length of the left subtree
// Recursive construction of left and right subtrees
root->left = build(preorder, left_pr + 1, left_pr + leftSize, postorder, left_po, mid);
root->right = build(preorder, left_pr + leftSize + 1, right_pr, postorder, mid + 1, right_po - 1);
return root;
}
struct TreeNode* constructFromPrePost(int* preorder, int preorderSize, int* postorder, int postorderSize){
return build(preorder, 0, preorderSize - 1, postorder, 0, postorderSize - 1);
}边栏推荐
- LeetCode刷题——验证二叉树的前序序列化#331#Medium
- . Net again! Happy 20th birthday
- [leetcode] 344 reverse string
- 04. Some thoughts on enterprise application construction after entering cloud native
- LeetCode_ String_ Simple_ 412.Fizz Buzz
- 6.12 critical moment of Unified Process Platform
- PTA ladder game exercise set l2-001 inter city emergency rescue
- PTA 天梯赛习题集 L2-001 城市间紧急救援
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- 彻底弄懂浏览器强缓存和协商缓存
猜你喜欢

【LeetCode】417-太平洋大西洋水流问题

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

语义分割学习笔记(一)

How does the computer set up speakers to play microphone sound

Download blender on Alibaba cloud image station

LeetCode刷题——递增的三元子序列#334#Medium

【LeetCode】1162-地图分析
![[leetcode] 1905 statistics sub Island](/img/82/d2f7b829f5beb7f9f1eabe8d101ecb.png)
[leetcode] 1905 statistics sub Island

JVM architecture, classloader, parental delegation mechanism

Steps for Navicat to create a new database
随机推荐
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
13_ Redis_ affair
Beijing rental data analysis
Force deduction solution summary 2029 stone game IX
College entrance examination score line climbing
Thoroughly understand browser strong cache and negotiation cache
【LeetCode】19-删除链表的倒数第N个结点
Bing.com網站
士官类学校名录
【LeetCode】200-岛屿数量
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
12_ Redis_ Bitmap_ command
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
[leetcode] 1254 - count the number of closed Islands
Bing. Com website
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
4. Data splitting of Flink real-time project
Solve the problem of frequent interruption of mobaxterm remote connection
Leetcode skimming - remove duplicate letters 316 medium