当前位置:网站首页>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;
}
}
边栏推荐
- Cnopendata geographical distribution data of religious places in China
- misc ez_ usb
- Hands on deep learning (IV) -- convolutional neural network CNN
- 【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
- Button wizard script learning - about tmall grabbing red envelopes
- Shell 脚本的替换功能实现
- QT learning 28 toolbar in the main window
- 微信小程序基本组件使用介绍
- Roulette chart 2 - writing of roulette chart code
- Zhilian + AV, AITO asked M7 to do more than ideal one
猜你喜欢

Codeforce c.strange test and acwing

快速使用 Jacoco 代码覆盖率统计

Quickly use Jacobo code coverage statistics

Bugku CTF daily one question chessboard with only black chess

Force buckle 144 Preorder traversal of binary tree

Why should we understand the trend of spot gold?

Linux server development, MySQL index principle and optimization
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

Linux server development, redis protocol and asynchronous mode

即刻报名|飞桨黑客马拉松第三期等你挑战
随机推荐
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
Linux server development, detailed explanation of redis related commands and their principles
C语言队列
解决问题:Unable to connect to Redis
Button wizard script learning - about tmall grabbing red envelopes
CTF daily question day43 rsa5
Linux server development, MySQL cache strategy
Padavan manually installs PHP
Ansible
paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
青龙面板--花花阅读
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Cnopendata list data of Chinese colleges and Universities
[experience sharing] how to expand the cloud service icon for Visio
Info | webrtc M97 update
芯片 设计资料下载
QT learning 26 integrated example of layout management
C语言通信行程卡后台系统
[unity] several ideas about circular motion of objects
Qt学习27 应用程序中的主窗口