当前位置:网站首页>Sword finger offer07 Rebuild binary tree
Sword finger offer07 Rebuild binary tree
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer07. Reconstruction of binary tree
Title Description
Enter the results of preorder traversal and inorder traversal of a binary tree , Build the binary tree and return its root node .
It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers .
Example 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Limit :
0 <= Number of nodes <= 5000
JAVA Code
Recursive method
Refer to the solution in tianqin book .
** design idea :** The first element in the sequence is traversed first x That is, the root node value of the tree . Found in middle order traversal x, utilize x Divide the array traversed in middle order into two subsequences , The left sequence forms the left subtree of the tree , The right sequence forms the right subtree of the tree , Then treat the left and right subsequences in the same way , Until there is only one element left in the processed subsequence , The construction of binary tree is completed .
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
TreeNode node = createTree(preorder,inorder,0,n-1,0,n-1);
return node;
}
// Building a binary tree
public TreeNode createTree(int[] preorder, int[] inorder,int L1,int R1,int L2,int R2){
TreeNode node;
int i;
if(L1>R1){
return null;
}
node = new TreeNode();
node.left=node.right=null;
for(i = L2;i<=R2;++i){
if(inorder[i]==preorder[L1]){
break;
}
}
node.val = inorder[i];
node.left = createTree(preorder,inorder,L1+1,L1+i-L2,L2,i-1);
node.right = createTree(preorder,inorder,L1+i-L2+1,R1,i+1,R2);
return node;
}
}
official – Iterative method
Iterative non recursive method .
The idea can be based on the non recursive implementation of binary tree traversal .
The non recursive method of binary tree is hard to understand , So I'll add it here when I have learning needs in the future .
For the time being, I feel that recursion is enough . I can't understand it, so I don't bother to waste time .
边栏推荐
- Shutter: overview of shutter architecture (excerpt)
- 242. Effective letter heteronyms
- Lambda表达式
- 【附下载】密码获取工具LaZagne安装及使用
- Official website of Unicode query
- DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
- PHP導出word方法(一mht)
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- Laravel time zone timezone
- (construction notes) grasp learning experience
猜你喜欢
shardingSphere分库分表<3>
剑指Offer10- I. 斐波那契数列
4000 word super detailed pointer
Symlink(): solution to protocol error in PHP artisan storage:link on win10
LeetCode 0556.下一个更大元素 III - 4步讲完
OpenGL draws colored triangles
实现验证码验证
Prompt unread messages and quantity before opening chat group
【mysql专项】读锁和写锁
为什么我的mysql容器启动不了呢
随机推荐
Qt+vtk+occt reading iges/step model
网络通讯之Socket-Tcp(一)
257. All paths of binary tree
Atomic atomic operation
SLF4J 日志门面
Official website of Unicode query
wpa_ cli
Flutter 退出登录二次确认怎么做才更优雅?
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Shell: basic learning
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
Socket TCP for network communication (I)
adb push apk
MySQL time zone solution
Php Export word method (One MHT)
How to deploy web pages to Alibaba cloud
C language improvement article (wchar_t) character type
PHP export word method (phpword)
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
AOSP ~ NTP (Network Time Protocol)