当前位置:网站首页>Recursive method to construct binary tree from preorder and inorder traversal sequence
Recursive method to construct binary tree from preorder and inorder traversal sequence
2022-07-07 08:04:00 【Hydrion-Qlz】
105. Construction of binary trees from traversal sequences of preorder and middle order
Medium difficulty
Given two arrays of integers preorder and inorder , among preorder It's a binary tree The first sequence traversal , inorder It's from the same tree In the sequence traversal , Please construct a binary tree and return its root node .
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 first node in the preorder traversal is the current node , Its left subtree and right subtree are on its right
We can find the current node according to the previous 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 the preceding traversal , But you can get this value from the middle order traversal
Specific please see 32 And 33 That's ok
package cn.edu.xjtu.carlWay.tree.preAndInConstructBinaryTree;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 105. Construction of binary trees from traversal sequences of preorder and middle order * Given two arrays of integers preorder and inorder , among preorder Is the prior traversal of a binary tree , inorder Is the middle order traversal of the same tree , Please construct a binary tree and return its root node . * <p> * https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ */
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helpBuild(preorder, 0, preorder.length, inorder, 0, inorder.length);
}
private TreeNode helpBuild(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {
if (inRight - inLeft == 0) {
return null;
}
if (inRight - inLeft == 1) {
return new TreeNode(inorder[inLeft]);
}
TreeNode root = new TreeNode(preorder[preLeft]);
int rootIndex = 0;
for (int i = inLeft; i < inRight; i++) {
if (inorder[i] == root.val) {
rootIndex = i;
break;
}
}
root.left = helpBuild(preorder, preLeft + 1, preLeft + 1 + (rootIndex - inLeft), inorder, inLeft, rootIndex);
root.right = helpBuild(preorder, preLeft + 1 + (rootIndex - inLeft), preRight, inorder, rootIndex + 1, inRight);
return root;
}
}
边栏推荐
- Linux server development, MySQL index principle and optimization
- MySQL multi column index (composite index) features and usage scenarios
- [UVM foundation] what is transaction
- Common method signatures and meanings of Iterable, collection and list
- Detailed explanation of Kalman filter for motion state estimation
- Qt学习26 布局管理综合实例
- buuctf misc USB
- Niu Mei's mathematical problem --- combinatorial number
- Redis technology leak detection and filling (II) - expired deletion strategy
- Linux server development, MySQL stored procedures, functions and triggers
猜你喜欢

央视太暖心了,手把手教你写HR最喜欢的简历

探索Cassandra的去中心化分布式架构

Force buckle 144 Preorder traversal of binary tree

Record a stroke skin bone error of the skirt

解决问题:Unable to connect to Redis

Wechat applet data binding multiple data
![[2022 actf] Web Topic recurrence](/img/e4/ab9a1771489d751ee73a79f151d374.png)
[2022 actf] Web Topic recurrence

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
![[SUCTF 2019]Game](/img/9c/362117a4bf3a1435ececa288112dfc.png)
[SUCTF 2019]Game

Linux server development, SQL statements, indexes, views, stored procedures, triggers
随机推荐
Téléchargement des données de conception des puces
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
微信小程序基本组件使用介绍
[mathematical notes] radian
Leanote private cloud note building
有 Docker 谁还在自己本地安装 Mysql ?
Implementation of replacement function of shell script
pytest+allure+jenkins环境--填坑完毕
Few-Shot Learning && Meta Learning:小样本学习原理和Siamese网络结构(一)
C语言航班订票系统
Yugu p1020 missile interception (binary search)
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
CTF daily question day43 rsa5
快速使用 Jacoco 代码覆盖率统计
C language communication travel card background system
追风赶月莫停留,平芜尽处是春山
[Stanford Jiwang cs144 project] lab3: tcpsender
Bugku CTF daily one question chessboard with only black chess
Linux Installation MySQL 8.0 configuration
[2022 actf] Web Topic recurrence