当前位置:网站首页>Jz36 binary search tree and bidirectional linked list
Jz36 binary search tree and bidirectional linked list
2022-07-26 06:10:00 【XH learning java】
Catalog
Introduction to the properties of binary search tree
Topic introduction
Topic link
JZ36 Binary search tree and double linked list
Title Description
Enter a binary search tree , The binary search tree is transformed into a sorted two-way linked list
Such as :

Title Requirements and data scope
Data range : Enter the number of nodes of the binary tree 0 <= n <= 1000, The value of each node in the binary tree 0 <= val <= 1000
requirement : The space complexity is O(1), Time complexity O(n)
Title Precautions
Request cannot create new node , Only the nodes in the tree can be adjusted to point , The left pointer of the node in the tree points to the precursor , Right pointer to successor
Returns the pointer of the first node in the linked list
Function return TreeNode, There are left and right pointers , In fact, it can be regarded as a two-way linked list
No need to output bidirectional linked list , The program will automatically print out according to the return value
Introduction to the properties of binary search tree
Binary search tree is also called binary sort tree , It has the following properties :
- If its left subtree is not empty , Then the value of the left subtree node is less than the value of the root node
- If its right subtree is not empty , Then the value of the right subtree node is greater than the value of the root node
- Its left and right subtrees are also binary search trees
Topic analysis
The topic is to transform a binary search tree into an ordered two-way linked list , From the properties of the binary search tree above, it is not difficult to get , The result of traversing the middle order of the binary search tree is just in the order from small to large , The topic requires nodes left The pointer points to the front drive ,right The pointer points to the successor , So the idea of making the question is as follows :
Link the nodes traversed in the middle order according to the requirements of the topic
How to solve the problem
Method 1
Drawing analysis process :

The specific steps are as follows :
- Write a method of middle order traversal , Link node
- The middle order traversal is implemented recursively , So define first on the outside of the method pre==null,pre Tag traversal root When the previous node
- First recursively traverse the left subtree
- let root.left == pre,if(pre != null) when , Give Way pre.right = root
- Finally, recursively traverse the right subtree
- The method given in the title should return to the first node , The first node is the leftmost node of the tree , First find the first node
- If the tree is empty , return null, If it's not empty , If the left subtree of the root is not empty , Continue to find the leftmost node with the left subtree of the root as the tree
- Call the above middle order traversal method
- Return to the first node
Method 2
Because the topic gives the maximum number of nodes is 1000, And the space complexity is required to be O(1), So we can store the nodes traversed in the middle order into an array , This array new The size of the space is 1000, because 1000 Is the specific value , So the space complexity also meets O(1), Then traverse the array and link the nodes in the array
The specific steps are as follows :
- new Space for 1000 An array of nodes
- Write a middle order traversal to store all the nodes in the tree in the array
- In the method given in the title , Call the above method first , Store the nodes in the array
- from index=0 Start traversing array , Give Way array[index+1].left = array[index],array[index].right = array[index+1]
Code implementation
Method one code
import java.util.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode pre = null;
public void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
root.left = pre;
if(pre != null){
pre.right = root;
}
pre = root;
inOrder(root.right);
}
public TreeNode Convert(TreeNode pRootOfTree) {
if(pRootOfTree == null){
return null;
}
TreeNode head = pRootOfTree;
while(head.left != null){
head = head.left;
}
inOrder(pRootOfTree);
return head;
}
}Method 2 code
import java.util.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode[] array = new TreeNode[1000];
int i = 0;
public void inOrder(TreeNode root){
if(root != null){
inOrder(root.left);
array[i++] = root;
inOrder(root.right);
}
}
public TreeNode Convert(TreeNode pRootOfTree) {
inOrder(pRootOfTree);
int index = 0;
while(array[index+1] != null){
array[index].right = array[index+1];
array[index+1].left = array[index];
index++;
}
return array[0];
}
}边栏推荐
- 英语句式参考纯享版 - 状语从句
- Read five articles in the evening | Economic Daily: don't treat digital collections as wealth making products
- 【Day_03 0420】数组中出现次数超过一半的数字
- Distributed | practice: smoothly migrate business from MYCAT to dble
- 【Day05_0422】C语言选择题
- Servlet无法直接获取request请求中的JSON格式数据
- Learn about spark project on nebulagraph
- ament_cmake生成ROS2库并链接
- Matlab vector and matrix
- Convolutional neural network (IV) - special applications: face recognition and neural style transformation
猜你喜欢
随机推荐
【Day03_0420】C语言选择题
Traversal of the first, middle, and last order of a binary tree -- Essence (each node is a "root" node)
日志收集分析平台搭建-1-环境准备
Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!
Kingbasees SQL language reference manual of Jincang database (8. Function (10))
WebAPI整理
Introduction to three feasible schemes of grammatical generalization
Unity2d animator cannot create transition
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
逆序打印链表
【2023杰理科技提前批笔试题】~ 题目及参考答案
Embedded sharing collection 14
Mobile web
Flex layout
【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
Solutions to the failure of copy and paste shortcut keys
[the most complete and detailed] ten thousand words explanation: activiti workflow engine
【Day_02 0419】排序子序列
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))








