当前位置:网站首页>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;
}
}
边栏推荐
- Numbers that appear only once
- Sign up now | oar hacker marathon phase III, waiting for your challenge
- Operation suggestions for today's spot Silver
- Content of string
- 2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
- The principle and implementation of buffer playback of large video files
- Yugu p1020 missile interception (binary search)
- 【VHDL 并行语句执行】
- The charm of SQL optimization! From 30248s to 0.001s
- [Stanford Jiwang cs144 project] lab3: tcpsender
猜你喜欢
Force buckle 144 Preorder traversal of binary tree
Cnopendata list data of Chinese colleges and Universities
2022制冷与空调设备运行操作复训题库及答案
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
[CV] Wu Enda machine learning course notes | Chapter 8
Main window in QT learning 27 application
[Stanford Jiwang cs144 project] lab3: tcpsender
有 Docker 谁还在自己本地安装 Mysql ?
JSON data flattening pd json_ normalize
Visualization Document Feb 12 16:42
随机推荐
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
C语言通信行程卡后台系统
这5个摸鱼神器太火了!程序员:知道了快删!
What is the interval in gatk4??
QT learning 26 integrated example of layout management
B. Value sequence thinking
2022年茶艺师(中级)考试试题及模拟考试
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
Lattice coloring - matrix fast power optimized shape pressure DP
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
3D reconstruction - stereo correction
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
[UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
[guess-ctf2019] fake compressed packets
Yugu p1020 missile interception (binary search)
Why should we understand the trend of spot gold?