当前位置:网站首页>Leetcode-297 serialization and deserialization of binary tree
Leetcode-297 serialization and deserialization of binary tree
2022-07-28 20:24:00 【z754916067】
subject
Ideas
- Record its preorder traversal and inorder traversal and length Building a binary tree
- The difficulty lies in building , It should be built recursively , I wrote it myself before , It seems that I can't write at all now …
- After a long time, I found that I ignored the situation of negative numbers and multiple numbers … Vomiting blood
Code
// You can save through traversal First record root As the first character The middle order traversal and the pre order traversal are recorded later
StringBuilder ans = new StringBuilder();
int length=0;
public String serialize(TreeNode root) {
// First record first order
pre(root);
// Record length
ans.insert(0,length);
ans.insert(1," ");
// In the sequence traversal
order(root);
return ans.toString();
}
public void pre(TreeNode root){
if(root==null) return;
ans.append(root.val+" ");
length++;
if(root.left!=null) pre(root.left);
if(root.right!=null) pre(root.right);
}
public void order(TreeNode root){
if(root==null) return;
if(root.left!=null) order(root.left);
ans.append(root.val+" ");
if(root.right!=null) order(root.right);
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] arr = data.split(" ");
// To obtain the length of the Intercepting string
int length = Integer.valueOf(arr[0]);
// Intercepting preorder and inorder strings
int[] pre =new int[length];
int[] order = new int[length];
for(int i=0;i<length;i++) pre[i] = Integer.valueOf(arr[1+i]);
for(int i=0;i<length;i++) order[i] = Integer.valueOf(arr[1+i+length]);
return Resume(pre,order);
}
public TreeNode Resume(int[] pre,int[] order){
if(order.length==0) return null;
// The root node
TreeNode root = new TreeNode(pre[0]);
// Root node found in order Position in You can find the position of its left and right subtrees
int local=0;
while (order[local] !=root.val) local++;
// Began to intercept Because it's an array So new
int[] left_pre = Arrays.copyOfRange(pre,1,pre.length);
int[] left_order = Arrays.copyOfRange(order,0,local);
int[] right_pre = Arrays.copyOfRange(pre,local+1,pre.length);
int[] right_ord = Arrays.copyOfRange(order,local+1,order.length);
root.left = Resume(left_pre,left_order);
root.right = Resume(right_pre,right_ord);
return root;
}
边栏推荐
- Linux Installation MySQL (pit filling version)
- Power Bi 2021 calendar DAX code
- 9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
- Does any elder brother know how to solve the huge flinksql log
- Circular linked list OJ question
- CM4 development cross compilation tool chain production
- Solve the brick stacking problem (DP)
- Implementation of strstr in C language
- DOS common commands
- Residual network RESNET source code analysis - pytoch version
猜你喜欢
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
[C language] function
【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
83.(cesium之家)cesium示例如何运行
Durham High Lord (classic DP)
Raspberry pie CM4 -- using metartc3.0 to integrate ffmpeg to realize webrtc push-pull streaming
[C language] step jumping problem [recursion]
MySQL startup error 1607 unexpected process termination
Maximum exchange [greedy thought & monotonic stack implementation]
Merge sort template
随机推荐
Raspberrypico serial communication
Raspberry connects EC20 for PPP dialing
1. C language variable type, global variable, local variable
User, user group related operations
[C language] simulation implementation of strlen (recursive and non recursive)
HSETNX KEY_ Name field value usage
跨区域网络的通信学习静态路由
Practice of real-time push demo of three web messages: long polling, iframe and SSE
Wildcard ssl/tls certificate
Common instructions of vim software
CM4 development cross compilation tool chain production
Usage of const and assert
Regular symbol description
New fruit naming (DP is similar to the longest common subsequence)
C language pointer and two-dimensional array
Maximum exchange [greedy thought & monotonic stack implementation]
Solve the cookie splitting problem (DP)
通配符 SSL/TLS 证书
CNN convolution neural network learning process (weight update)
How to use pycharm to quickly create a flask project