当前位置:网站首页>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 .
边栏推荐
- C language improvement article (wchar_t) character type
- Cloud Computing future - native Cloud
- 111. Minimum depth of binary tree
- TOGAF认证自学宝典V2.0
- Why can't my MySQL container start
- [learning notes] DP status and transfer
- Computer version wechat applet full screen display method, mobile phone horizontal screen method.
- 20. Valid brackets
- JVM内存模型
- (构造笔记)GRASP学习心得
猜你喜欢

LeetCode 0556. Next bigger element III - end of step 4

4000字超详解指针

OpenGL index cache object EBO and lineweight mode

Implement verification code verification

TOGAF认证自学宝典V2.0

ES6新特性

Why can't my MySQL container start

Shutter widget: centerslice attribute

Talk about the state management mechanism in Flink framework

If you can't learn, you have to learn. Jetpack compose writes an im app (I)
随机推荐
257. All paths of binary tree
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
Kubernetes three dozen probes and probe mode
If you can't learn, you have to learn. Jetpack compose writes an im app (I)
[embedded] - Introduction to four memory areas
Self made pop-up input box, input text, and click to complete the event.
雲計算未來 — 雲原生
AOSP ~ NTP (Network Time Protocol)
20. Valid brackets
ES6 standard
Adult adult adult
Use of QT OpenGL camera
PHP导出word方法(一phpword)
Prompt unread messages and quantity before opening chat group
Fundamentals of concurrent programming (III)
Shutter widget: centerslice attribute
242. Effective letter heteronyms
LeetCode 0556.下一个更大元素 III - 4步讲完
Flutter Widget : KeyedSubtree
【嵌入式】---- 内存四区介绍