当前位置:网站首页>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;
}
}
边栏推荐
- [UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
- Most elements
- LeetCode 90:子集 II
- Content of string
- C语言航班订票系统
- Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
- PHP exports millions of data
- C语言队列
- What is the interval in gatk4??
- 2022年茶艺师(中级)考试试题及模拟考试
猜你喜欢
央视太暖心了,手把手教你写HR最喜欢的简历
自定义类加载器加载网络Class
Custom class loader loads network class
Linux server development, SQL statements, indexes, views, stored procedures, triggers
[Stanford Jiwang cs144 project] lab4: tcpconnection
即刻报名|飞桨黑客马拉松第三期等你挑战
Yugu p1020 missile interception (binary search)
MySQL multi column index (composite index) features and usage scenarios
有 Docker 谁还在自己本地安装 Mysql ?
[guess-ctf2019] fake compressed packets
随机推荐
Common validation comments
Rust versus go (which is my preferred language?)
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
Regular e-commerce problems part1
贝叶斯定律
Chip design data download
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Quickly use Jacobo code coverage statistics
Bugku CTF daily one question chessboard with only black chess
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
解决问题:Unable to connect to Redis
[CV] Wu Enda machine learning course notes | Chapter 8
Introduction to basic components of wechat applet
央视太暖心了,手把手教你写HR最喜欢的简历
Operation suggestions for today's spot Silver
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
CTF daily question day43 rsa5
Installing postgresql11 database under centos7
json 数据展平pd.json_normalize
2022 tea master (intermediate) examination questions and mock examination