当前位置:网站首页>Recursive method constructs binary tree from middle order and post order traversal sequence
Recursive method constructs binary tree from middle order and post order traversal sequence
2022-07-07 08:04:00 【Hydrion-Qlz】
106. Construct binary tree from middle order and post order traversal sequence
Medium difficulty
Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree .
Ideas
The knowledge points that need to be understood are :
- In the middle order traversal, the left side of a node is all nodes of its left subtree , On the right are all the nodes of its right subtree
- The last one in the subsequent traversal is the current node , Its left subtree and right subtree are on its left
We can find the current node according to the subsequent traversal , Then find the position of the current root node in the ordered array , Then the range of the new left subtree and right subtree is determined by the position of the root node and the range of its subtree , Then recurs continuously , Until the last traversal of the entire array .
We cannot determine the range of left subtree and right subtree in subsequent traversal , But you can get this value from the middle order traversal
Specific please see 37 And 38 That's ok
package cn.edu.xjtu.carlWay.tree.postAndInConstructBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 106. Construct binary tree from middle order and post order traversal sequence * Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree . * <p> * https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ */
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return helpBuild(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode helpBuild(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight) {
// There are no elements
if (inRight - inLeft < 1) {
return null;
}
// There's only one element
if (inRight - inLeft == 1) {
return new TreeNode(inorder[inLeft]);
}
// The last element in the subsequent array is the root node
int rootVal = postorder[postRight - 1];
TreeNode root = new TreeNode(rootVal);
// Find the position of the root node in the ordered array
int rootIndex = 0;
for (int i = inLeft; i < inRight; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
root.left = helpBuild(inorder, inLeft, rootIndex, postorder, postLeft, postLeft + (rootIndex - inLeft));
root.right = helpBuild(inorder, rootIndex + 1, inRight, postorder, postLeft + (rootIndex - inLeft), postRight - 1);
return root;
}
}
边栏推荐
- C language flight booking system
- You Li takes you to talk about C language 6 (common keywords)
- Zhilian + AV, AITO asked M7 to do more than ideal one
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
- Rust Versus Go(哪种是我的首选语言?)
- Li Kou interview question 04.01 Path between nodes
- QT learning 26 integrated example of layout management
- These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
- Cnopendata list data of Chinese colleges and Universities
- 【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
猜你喜欢

QT learning 28 toolbar in the main window

即刻报名|飞桨黑客马拉松第三期等你挑战
![[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

运放电路的反馈电阻上并联一个电容是什么作用

misc ez_ usb

Common validation comments
![[2022 ciscn] replay of preliminary web topics](/img/1c/4297379fccde28f76ebe04d085c5a4.png)
[2022 ciscn] replay of preliminary web topics

Cnopendata list data of Chinese colleges and Universities
![[CV] Wu Enda machine learning course notes | Chapter 8](/img/c0/7a39355fb3a6cb506f0fbcf2a7aa24.jpg)
[CV] Wu Enda machine learning course notes | Chapter 8
随机推荐
JSON data flattening pd json_ normalize
Open source ecosystem | create a vibrant open source community and jointly build a new open source ecosystem!
微信小程序基本组件使用介绍
[unity] several ideas about circular motion of objects
Info | webrtc M97 update
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
贝叶斯定律
2022 tea master (intermediate) examination questions and mock examination
Operation suggestions for today's spot Silver
2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
Record a stroke skin bone error of the skirt
Hands on deep learning (IV) -- convolutional neural network CNN
Linux server development, MySQL stored procedures, functions and triggers
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
LeetCode 40:组合总和 II
Ansible
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
快速使用 Jacoco 代码覆盖率统计
Thinkcmf6.0 installation tutorial
Linux server development, MySQL cache strategy