当前位置:网站首页>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];
}
}边栏推荐
- 【Day04_0421】C语言选择题
- 二叉树的前中后序遍历——本质(每个节点都是“根”节点)
- Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
- Latex同时合并表格的多行多列
- C language explanation series - comprehensive exercises, guessing numbers games
- Optical quantum milestone: 3854 variable problems solved in 6 minutes
- flex布局
- Youwei low code: Brick life cycle component life cycle
- Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
- Widget is everything, widget introduction
猜你喜欢

逆序打印链表

Etcd database source code analysis - cluster membership changes log

Unity2D 动画器无法 创建过渡

招标信息获取

Day110. Shangyitong: gateway integration, hospital scheduling management: Department list, statistics based on date, scheduling details

Optical quantum milestone: 3854 variable problems solved in 6 minutes
![[Oracle SQL] calculate year-on-year and month on month (column to row offset)](/img/ee/59d050e03c2a4ba04de57df1322283.png)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

Introduction to three feasible schemes of grammatical generalization

Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
随机推荐
Latex同时合并表格的多行多列
【Day_02 0419】排序子序列
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
【Day_03 0420】数组中出现次数超过一半的数字
Acquisition of bidding information
漫谈软件缺陷管理的实践
H. Take the elevator greedy
JDBC streaming query and cursor query
[SQL optimization] (big table tips) sometimes a 2-hour SQL operation may take only 1 minute
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
VRRP principle and basic commands
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
数据库sql语言实战
【Day_06 0423】不要二
Print linked list in reverse order
ETCD数据库源码分析——Cluster membership changes日志
Embedded sharing collection 15
Solution to slow download speed of vagrant
2022年下半年系统集成项目管理工程师(软考中级)报名条件
unity 像素画导入模糊问题